Alternative syntax in PHP (colon, endif, endwhile, endfor)

Source: Internet
Author: User
Tags php foreach
Alternative syntax in PHP (colon, endif, endwhile, endfor) we often see many strange PHP syntaxes in the templates of blog programs such as wordpress, such:

Empty

For a considerable number of PHP enthusiasts, what are these things? Are those blog developers doing their own PHP-like template language?

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 PHP process control syntax. What should replace the 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, for, foreach, switch) statements have alternative syntax.


The basic form of alternative syntax:

Replace the left curly braces ({) with a colon (:), and change the right curly braces (}) with endif, endwhile, endfor, endforeach, and endswitch respectively;


Let's give a column:

Less than 0

The preceding statement is equivalent

Less than 0


Why is it almost invisible in pure PHP code?

These syntaxes are a bit different from the C family's tradition. you are not very familiar with these syntaxes and are not very convenient.


Everyone is not used to it. what is the role of such an alternative?

The existence is reasonable, and it is free of use. these syntaxes can be used in the code of the 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. the process control logic is clearer and the code is easier to read. 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:

Equal to 5



Equal to 5



5 or 6



While alternative syntax:

  • Loop point



  • For alternative syntax:


  • Loop point


  • Foreach alternative syntax:

  • Loop point


  • Switch substitution syntax:
    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.