Alternative syntax in PHP (colon, endif, Endwhile, ENDfor)

Source: Internet
Author: User
Tags php foreach php template

We often see a lot of strange php syntax in a WordPress-Class blog program template, such as: <?php if (Empty ($_get[' A])):?> <font color = "Red" > Empty </fo Nt> <?

PHP endif;? > For a considerable part of the PHP enthusiasts have never seen, what is these things? Are those blogging developers doing their own PHP template language? Not also. In fact, these are PHP syntax, just not used very often, these are PHP process control of the alternative syntax.

Here we give you a specific word on PHP process Control alternative syntax. What's the alternative to grammar? In short, it is some kind of grammatical alternative notation.


what syntax in PHP has an alternative syntax? Process Control (including if, while, for, foreach, switch) has alternative syntax for several statements.


the basic form of substitution syntax: the left curly brace ({) is replaced by a colon (:) and the right curly brace (}) is replaced by endif, Endwhile, ENDfor, Endforeach, and Endswitch;
Give me a column: <?php if ($a <0):?

> less than 0 <?php endif;?

> The above statement is equivalent to <?php if ($a <0) {?> less than 0 <?php}?

>
Why is the pure PHP code almost invisible? These grammars are a bit out of line with the C family tradition, and we're not used to the syntax, and it's not very convenient.
We're not used to it. And what kind of role does he have? Existence is reasonable. It is free of its usefulness, where these grammars can be played in the code of the PHP and HTML hybrid pages.

Advantages such as the following: 1. Make the HTML and PHP Mix page code cleaner and neater. A friend with code cleanliness is most afraid of a messy mix of code, with these no curly braces alternative syntax, you love clean friends happy to pee.

2. The Process control logic is clearer. The code is easier to read. To change someone else's PHP and HTML mixed code. Open discovery. I wipe! It's so damn rubbish!

Assuming the alternative syntax, I think the garbage program developers are not too messy to write. 3. Some friends from other basic language families, such as ASP, will be easier to use PHP.

It's useless to talk for half a day, okay? How does this thing work?

Based on the usage of the previous descriptive narrative, the substitution syntax for the IF statement is used such as the following:
<?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):?>
<li> Cycle Point What </li>
<?

PHP endwhile;?>

For substitution syntax:

<?php for (EXPR1; expr2; expr3):?

>
<li> Cycle Point What </li>
<?php endfor;?>
foreach substitution syntax:
<?

PHP foreach (expr1):?>
<li> Cycle Point 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;
?>

Alternative syntax in PHP (colon, endif, Endwhile, ENDfor)

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.