Alternative syntax for PHP process Control

Source: Internet
Author: User
Tags mixed php and php code php foreach

Here is a detailed talk about PHP Process Control alternative syntax. What is an alternative syntax?
In short, it is the alternative writing of some grammar.
Do the syntax in PHP have alternative syntax?
Process Control (including If,while,foreach,switch) These statements have alternative syntax.
The basic form of alternative syntax:
The left curly brace ({) is replaced with a colon (:), and the right curly brace (}) is replaced with Endif;,endwhile;,endfor;,endforeach; and Endswitch;
Let's take an example:

Let's take an example:
<?php if ($a <0):?>
It's negative, pull.
<?php endif;?>
The above statement is equivalent to
<?php if ($a <0) {?>
It's negative, pull.
<?php}?>

It's negative, pull.

Why is it almost invisible inside the pure PHP code?

These grammars are a little different from the C family tradition, and we are not accustomed to this kind of grammar, and not very convenient
People are not used to it, and what is the use of such an alternative to him? Does the egg hurt?
The existence is reasonable, it has its own use, these grammar can play place in the PHP and HTML mixed page code inside. The benefits are as follows:
1. Make HTML and PHP mixed page code more clean and tidy.
Code neat Friends are most afraid of a messy mix of code, with these no curly braces alternative syntax, you love clean friends happy to the urine shock.
2. Process Control logic clearer, code easier to read
To change someone else's PHP and HTML mix Code, open discovery, I rub! It's too damn rubbish! If you use the alternative syntax, I think the garbage program developers will not write too messy.
3. Some friends who are transferred from other basic language families such as ASP will make PHP easier to use.
It's useless to talk for a while, okay? How does this thing work?
The alternative syntax for an IF statement is used as follows, based on the use method described previously:

<?php if ($a = 5):?>
<div> equals 5</div>
<?php ElseIf ($a = = 6):?>
<div> equals 5</div>
<?php Else:?>
<div> either 5 or 6</div>
<?php endif;?>

While substitution syntax:

<?php while (expr):?>
<li> Circular Dot What </li>
<?php Endwhile;?>

For substitution syntax:

<?php for (EXPR1; expr2; expr3):?>
<li> Circular Dot What </li>
<?php endfor;?>

foreach substitution syntax:

<?php foreach (expr1):?>
<li> Circular Dot 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;
?>

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.