Alternative syntax in PHP (colon, endif, endwhile, endfor), endwhileendfor
We often see many strange PHP syntaxes In the templates of blog programs such as wordpress, such as: <? Php if (empty ($ _ GET ['a]):?> <Font color = "red"> Empty </font> <? Php endif;?> For a considerable number of PHP enthusiasts, what are these things? Are those blog developers doing their own PHP-like template language? None. In fact, these are PHP syntaxes, but they are not commonly used. They are all alternative syntaxes for PHP process control. Here we will give you a detailed description of the alternative PHP process control syntax. What should replace the syntax? Simply put, it is an alternative way of writing some syntaxes.
Which of the following statements in PHP have alternative syntaxes?Process control (including if, while, for, foreach, switch) statements have alternative syntax.
The basic form of alternative Syntax:Replace the left curly braces ({) with a colon (:), and change the right curly braces (}) with endif, endwhile, endfor, endforeach, and endswitch respectively;
Let's give a column: <? Php if ($ a <0):?> Less than 0 <? Php endif;?> The preceding statement is equivalent to <? Php if ($ a <0) {?> Less than 0 <? Php }?>
Why is it almost invisible in pure PHP code?These syntaxes are a bit different from the C family's tradition. You are not very familiar with these syntaxes and are not very convenient.
Everyone is not used to it. What is the role of such an alternative?The existence is reasonable, and it is free of use. These syntaxes can be used in the code of the PHP and HTML hybrid pages. Benefits: 1. Make the HTML and PHP mixed Page code clean and tidy. Friends who have code cleansing are most afraid of mixed code. With these alternative syntaxes without curly braces, you may be happy to have a natural sensation. 2. The process control logic is clearer and the code is easier to read. I want to change the mixed PHP and HTML code of others. I will clean it! Too much TMD junk! If the alternative syntax is used, I don't think it's too messy for junk program developers. 3. Some friends who transfer from ASP and other basic language families will be more likely to use PHP.
It's useless for half a day. What can I do? How to use this item?
According to the previous usage method, 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 or 6 </div>
<? Php endif;?>
While alternative Syntax:
<? Php while (expr):?>
<Li> cyclic Points </li>
<? Php endwhile;?>
For Alternative Syntax:
<? Php for (expr1; expr2; expr3):?>
<Li> cyclic Points </li>
<? Php endfor;?>
Foreach alternative Syntax:
<? Php foreach (expr1):?>
<Li> cyclic Points </li>
<? Php endforeach;?>
Switch substitution 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;
?>
What is the error in the php statement I wrote?
PHP provides alternative syntaxes for process control, including if, while, for, foreach, and switch. The basic form of the alternative syntax is to replace left curly braces ({) with colons (:) and right curly braces (}) with endif;, endwhile;, endfor;, endforeach; and endswitch ;.
You did not add a semicolon
PHP help? <? Php endif;?> What's wrong?
<Div id = "container">
<? Php if (have_post ():?>
<? Php while (have_post ():?>
<? Php the_post ();?>
<? Php the_title ();?>
<? Php endwhile;?>
<? Php endif;?>
</Div>
Note: It is a colon, not a semicolon.