Introduction to the use of colons, endif, endwhile, and endfor in PHP

Source: Internet
Author: User
Tags php foreach wordpress blog
Some of the following uses have never been seen by a considerable number of PHP enthusiasts. What are these things? We often see many strange PHP syntaxes in the templates of wordpress blog programs, such:

The code is as follows:



Empty


For a considerable number of PHP enthusiasts, what are these things? Are those blog developers doing their own PHP-like template languages?
None. In fact, these are PHP syntaxes, but they are not commonly used. they are all alternative syntaxes for PHP process control.
Here we will give you a detailed description of the alternative syntax of PHP process control. What is an alternative syntax?
Simply put, it is an alternative way of writing some syntaxes.

Which of the following statements in PHP have alternative syntaxes?
Process control (including if, while, forforeach, switch) statements have alternative syntax.

The basic form of alternative syntax:
Replace Left curly braces ({) with colons (:), and change right curly braces (}) with endif;, endwhile;, endfor;, endforeach; and endswitch;

For example:

The code is as follows:



Is a negative number.

The preceding statement is equivalent

Is a negative number.



Why is it almost invisible in pure PHP code?
These syntaxes are a little different from the C family's tradition. they are not used to these syntaxes and are not very convenient.

Everyone is not used to it. what is the use of this alternative? Does it hurt?
Existence is reasonable, and it has its own use. these syntaxes can be used in the code of PHP and HTML hybrid pages. Benefits:
1. make the HTML and PHP mixed page code clean and tidy.
Friends who have code cleansing are most afraid of mixed code. with these alternative syntaxes without curly braces, you may be happy to have a natural sensation.
2. clearer process control logic and easier code reading
I want to change the mixed PHP and HTML code of others. I will clean it! Too much TMD junk! If the alternative syntax is used, I don't think it's too messy for junk program developers.
3. some friends who transfer from ASP and other basic language families will be more likely to use PHP.

It's useless for half a day. what can I do? How to use this item?

According to the previous usage method, the alternative syntax of the if statement is as follows:

The code is as follows:



Equal to 5



Equal to 5



5 or 6




While alternative syntax:

The code is as follows:



  • Loop point



  • For alternative syntax:

    The code is as follows:



  • Loop point



  • Foreach alternative syntax:

    The code is as follows:



  • Loop point




  • Switch substitution syntax:

    The code is as follows:


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