Example of alternative PHP process control syntax

Source: Internet
Author: User
Tags php foreach wordpress blog

Example of alternative PHP process control syntax

PHPer, a template of a wordpress blog program, will see many strange PHP syntaxes, such:
<? Php if (empty ($ GET _ ['a']):?>
<Font color = "red"> Empty </font>
<? Php endif;?>
What are these things that some PHPer never saw? Are those blog developers doing their own PHP-like template languages?
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 Syntax of PHP process control. What is an alternative 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, foreach, switch) statements have alternative syntax.
The basic form of alternative Syntax:
Replace left curly braces ({) with colons (:), and change right curly braces (}) with endif;, endwhile;, endfor;, endforeach; and endswitch;
For example:

Is a negative number.

The preceding statement is equivalent

Is a negative number.
Why is it almost invisible in pure PHP code?
These syntaxes are a little different from the C family's tradition. They are not used to these syntaxes and are not very convenient.
Everyone is not used to it. What is the use of this alternative? Does it hurt?
Existence is reasonable, and it has its own use. These syntaxes can be used in the code of 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. Clearer process control logic and easier code reading
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;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.