Introduction to the use of colon, endif, endwhile, endfor in PHP _php tips

Source: Internet
Author: User
Tags mixed php foreach
We often see a lot of strange php syntax in the wordpress type Blog program template, such as:
Copy Code code as follows:

<?php if (Empty ($GET _[' A ')):?>
<font color= "Red" > Empty </font>
<?php endif;?>

For a considerable part of the PHP enthusiasts have not seen Ah, what are these things? 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 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,forforeach,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:
Copy Code code as follows:

<?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}?>


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:
Copy Code code as follows:

<?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:
Copy Code code as follows:

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

For substitution syntax:
Copy Code code as follows:

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

foreach substitution syntax:
Copy Code code as follows:

<?php foreach (expr1):?>
<li> Circular Dot What </li>
<?php Endforeach;?>


Switch substitution syntax:
Copy Code code as follows:

<?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.