We often see a lot of strange php syntax in the wordpress template of a blog program, such as:
<?php if (Empty ($GET _[' A ')):?>
<font color= "Red" > Empty </font>
<?php endif;?>
For a lot of PHP enthusiasts have never seen Ah, what are these things? Are those blog developers doing their own php-like template language?
Non-also, in fact, these are PHP syntax, but not commonly used, these are PHP process control of the alternative syntax.
Here we will give you a detailed talk about PHP Process Control alternative syntax. What is an alternative syntax?
In short, it is some kind of grammatical alternative notation.
Is there an alternative syntax for those grammars in PHP?
Process Control (including If,while,forforeach,switch) has alternative syntax for several statements.
The basic form of alternative syntax:
The left curly brace ({) is replaced by a colon (:) and the right curly brace (}) is replaced by a endif;,endwhile;,endfor;,endforeach; and Endswitch;
Let's take an example:
<?php if ($a <0):?>
is negative pull
<?php endif;?>
The above statement is equivalent to
<?php if ($a <0) {?>
is negative pull
<?php}?>
Why is the pure PHP code almost invisible?
These grammars are a bit out of line with the C family tradition a little bit different, we are not accustomed to this grammar, and not very convenient
We're not used to it, and what's the use of this alternative? Does the egg hurt?
Existence is reasonable, it has its own usefulness, these grammars can be played in the PHP and HTML mixed page code inside. The benefits are as follows:
1. Make the HTML and PHP Mix page code cleaner and neater.
A friend with code cleanliness is most afraid of a messy mix of code, with these no curly braces alternative syntax, you love clean friends happy to pee.
2. Process Control logic clearer, code easier to read
To change someone else's PHP and HTML mixed code, open Find, I wipe! It's so damn rubbish! If you use the alternative syntax, I think the garbage program developers are not too messy to write.
3. Some friends from other basic language families, such as ASP, will be more likely to use PHP.
It's useless to talk for half a day, okay? How does this thing work?
The alternative syntax for the IF statement is used as follows, as described previously:
<?php if ($a = = 5):?>
<div> equals 5</div>
<?php ElseIf ($a = = 6):?>
<div> equals 5</div>
<?php Else:?>
<div> not 5 is 6</div>
<?php endif;?>
While substitution syntax:
<?php while (expr):?>
<li> Cycle Point What </li>
<?php Endwhile;?>
For substitution syntax:
<?php for (EXPR1; expr2; expr3):?>
<li> Cycle Point What </li>
<?php endfor;?>
foreach substitution syntax:
<?php foreach (expr1):?>
<li> Cycle Point What </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 am not equal to 0, 1 or 2";
Endswitch;
?>
What are the colons, endif, Endwhile, endfor in PHP?