Home  >  Article  >  Backend Development  >  What is alternative syntax in php? How is it used?

What is alternative syntax in php? How is it used?

伊谢尔伦
伊谢尔伦Original
2017-06-22 09:34:071762browse

Alternative syntax is a concept that is not commonly seen in PHP programming, but is sometimes very important.

Many friends will have this question: What do the colon and endif after else represent? After a quick Google search, you can understand that this is the alternative syntax for PHP.

The colon (:) is equivalent to the left curly brace ({), and endif is equivalent to the right curly brace (});

Another example:

<?php if ($a<0): ?>
//负数的情况
<?php endif; ?>

The above statement is equivalent to:

<?php if ($a<0){ ?>
//负数的情况
<?php } ?>

So what are the alternative syntaxes in PHP?

Flow control (including if, while, forforeach, switch) have alternative syntax for these statements.

Basic form of alternative syntax:

Replace the left curly brace ({) with a colon (:), and replace the right curly brace (}) with endif;, endwhile;, endfor;, endforeach; and endswitch;

while alternative syntax:

<?php while (expr): ?>
 <li>循环内容</li>
<?php endwhile; ?>

Other alternative syntax can be analogized.

Why is it almost invisible in pure PHP code?

These syntaxes are a bit inconsistent with the tradition of the C family and are a bit different. Everyone is not used to this syntax, and it is not very convenient.

Everyone is not used to it, and it is so different. What is the use? Does your balls hurt?

Existence is reasonable, and it has its own uses. The place where these syntaxes can be used is in the code of a mixed PHP and HTML page. The benefits are as follows:

1. Make the HTML and PHP mixed page code cleaner and neater.

What those friends who are obsessed with coding are most afraid of is messy mixed code. With these alternative syntaxes without braces, all friends who love cleanliness will be so happy that they pee.

2. The process control logic is clearer and the code is easier to read

If you want to change someone else’s PHP and HTML mixed code, open it and find it, I will delete it! What a fucking rubbish! If alternative syntax is used, I think no matter how rubbish the program developer is, his writing will not be too messy.

3. Some friends who have transferred from ASP and other basic language families will find it easier to use PHP.

It’s useless to talk for a long time, how about we get something to do? How to use this thing?

According to the usage method described previously, the alternative syntax of the if statement is as follows:

<?php if ($a == 5): ?> 
<div>等于5</div> 
<?php elseif ($a == 6): ?> 
<div>等于5</div> 
<?php else: ?> 
<div>不是5就是6</div> 
<?php endif; ?>

while alternative syntax:

<?php while (expr): ?> 
<li>循环点什么</li> 
<?php endwhile; ?>

for alternative syntax:

<?php for (expr1; expr2; expr3): ?> 
<li>循环点什么</li> 
<?php endfor; ?>

foreach alternative syntax:

<?php foreach (expr1): ?> 
<li>循环点什么</li> 
<?php endforeach; ?>

switch alternative syntax:

<?php 
switch ($i): 
case 0: 
echo "i equals 0"; 
break; 
case 1: 
echo "i equals 1"; 
break; 
case 2: 
echo "i equals 2"; 
break; 
default: 
echo "i is not equal to 0, 1 or 2"; 
endswitch; 
?>

The above is the detailed content of What is alternative syntax in php? How is it used?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn