Examples of alternative syntax for PHP process Control

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

  Phper will see a lot of strange PHP syntax, such as:
<?php if empty ($GET _[' a ')) as a template for WordPress-type Blog programs:
<font color = "Red" > Empty </font>
<?php endif
for a part of Phper have not seen these things at all? Are those blogging developers doing their own scripting language for PHP?
not also, in fact these are all PHP syntax, but not commonly used, these are PHP Process control alternative syntax.
Here is a detailed look at the alternative syntax for PHP process control. What is an alternative syntax? The
is simply the alternative way of writing some grammar.
does the syntax in PHP have an alternate syntax?
Process Control (including If,while,foreach,switch) has an alternative syntax for these statements.
The basic form of an 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
For example:

is a negative number

The above statement equals

is negative pull
Why is it almost invisible inside the pure PHP code?
These grammars are somewhat different from the C family tradition, and we are not accustomed to this syntax, and not very convenient
everyone is not accustomed to, and so the alternative to him what use? Does the egg hurt?
Existence is reasonable, it has its own usefulness, the syntax can be played in the PHP and HTML mixed page code inside. The benefits are as follows:
1. Make HTML and PHP mixed page code cleaner and more neat.
A friend with code cleanliness is most afraid of a mess of mixed 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 classes of basic languages such as ASP will make PHP easier to use.
It's no use talking for half a day, okay? How does this thing work?
According to the use method described previously, the alternative syntax for the IF statement is used as follows:
<?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):? &G T
<li> Loop dot what </li>
<?php endwhile
for alternative syntax:
<?php for (expr1; expr2; expr3):? > <li> Loop dot what </li>
<?php endfor;
foreach substitution syntax:
<?php foreach (expr1):?
<li > Loop 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.