What is the alternative syntax in PHP? How does it work?

Source: Internet
Author: User
Tags php foreach php programming php switch
alternative syntax is an uncommon and sometimes important concept in PHP programming.

Many friends have this question: what does the colon and endif after else mean? Google a bit to understand that this is the PHP alternative syntax.

where the colon (:) equivalent to the left curly brace ({), endif equivalent less right operand curly braces (});

Let me give you an example:

<?php if ($a <0): >//negative case <?php endif;?>

The above statement is equivalent to:

<?php if ($a <0) {? >//negative case <?php}?>

So the syntax in PHP has an alternative syntax?

Process Control (including If,while,forforeach,switch) has alternative syntax for several statements.

The basic form of alternative syntax:

The left curly brace ({) is replaced by a colon (:) and the right curly brace (}) is replaced by a endif;,endwhile;,endfor;,endforeach; and Endswitch;

While substitution syntax:

<?php while (expr):?> <li> cyclic content </li><?php endwhile;?>

Other alternative syntaxes can be analogous.

Why is the pure PHP code almost invisible?

These grammars are a bit out of line with the C family tradition a little bit different, we are not accustomed to this grammar, and not very convenient

We're not used to it, and what's the use of this alternative? Does the egg hurt?

Existence is reasonable, it has its own usefulness, these grammars can be played in the PHP and HTML mixed page code inside. The benefits are as follows:

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. Process Control logic clearer, code easier to read

To change someone else's PHP and HTML mixed code, open Find, I wipe! It's so damn rubbish! If you use 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 more likely to use PHP.

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

The alternative syntax for the IF statement is used as follows, as described previously:

<?php if ($a = = 5):?> <div> equals 5</div> <?php elseif ($a = = 6):?> <div> equals 5</div> <?p HP Else:?> <div> not 5 is 6</div> <?php endif;?>

While substitution syntax:

<?php while (expr):?> <li> Loop 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> 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 was not equal to 0, 1 or 2"; Endswitch;?>
Related Article

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.