We often see a lot of strange PHP syntax in templates of blogging programs such as wordpress, such as:
Copy code The code is as follows:
empty
php endif; ?>
For quite a few PHP enthusiasts, they have never seen it before. What are these? Could it be that the blog developers themselves created a template language that imitates PHP?
No, in fact, these are PHP syntax, but they are not commonly used. These are alternative syntaxes for PHP process control.
Here I will give you a detailed explanation of the alternative syntax for PHP process control. What is alternative syntax?
To put it simply, it is some alternative ways of writing grammar.
What are the alternative syntaxes in PHP?
Flow control (including if, while, forforeach, switch) statements have alternative syntax.
Basic form of alternative syntax: Replace the left curly brace ({) with a colon (:), replace the right curly brace (}) with endif;, endwhile;, endfor;, endforeach; and endswitch;
For example:
Copy the code The code is as follows:
is a negative number
The above statement is equivalent to
is a negative number
Why pure Hardly visible in PHP code? These syntaxes are a bit inconsistent with the tradition of the C family and are a bit offbeat. Everyone is not used to this syntax, and it is not very convenient.
Everyone is not used to it, and what’s the use of being so offbeat? ? 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?
Based on the previously described usage, the alternative syntax for the if statement is as follows:
Copy the code The code is as follows:
equal to 5
Equal to 5
Either 5 or 6
while alternative syntax:
Copy code The code is as follows:
What to loop
for alternative syntax:
Copy code The code is as follows:
What to loop
foreach Alternative syntax:
Copy code The code is as follows:
What to loop
switch alternative syntax:
Copy code The code is as follows:
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;
?>
http://www.bkjia.com/PHPjc/321663.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321663.htmlTechArticleWe often see a lot of strange PHP syntax in the templates of blogging programs such as wordpress, such as: Copy code code As follows: ?php if(empty($GET_['a'])): ? font color="red"empty/f...