Home > Backend Development > PHP Tutorial > Alternative syntax example for PHP flow control_PHP Tutorial

Alternative syntax example for PHP flow control_PHP Tutorial

WBOY
Release: 2016-07-13 10:16:49
Original
1094 people have browsed it

Examples of alternative syntax for PHP process control

PHPers who have learned about the templates of blogging programs such as wordpress will see a lot of strange PHP syntax, such as:

empty

For some PHPers, what are these things that they have never seen before? Could it be that the blog developers themselves created a template language that imitates PHP?
No, in fact, these are PHP syntax, but they are not commonly used. These are alternative syntaxes for PHP process control.
Here I will give you a detailed explanation of the alternative syntax for PHP process control. What is alternative syntax?
Simply put, they are some alternative ways of writing grammar.
What are the alternative syntaxes in PHP?
There are alternative syntaxes for flow control (including if, while, foreach, switch) statements.
Basic form of alternative syntax:
Replace the left curly brace ({) with a colon (:), and replace the right curly brace (}) with endif;, endwhile;, endfor;, endforeach; and endswitch;
Let’s give an example:

It’s a negative number

The above statement is equivalent to

It’s a negative number
Why is it almost invisible in pure PHP code?
These syntaxes are a bit inconsistent with the tradition of the C family and are a bit different. People are not used to this syntax, and it is not very convenient
No one is used to it, and what's the use of having him so different? Does your balls hurt?
Existence is reasonable, and it has its own uses. The place where these syntaxes can be used is in the code of a mixed PHP and HTML page. The benefits are as follows:
1. Make the HTML and PHP mixed page code cleaner and neater.
What those friends who have a code fetish are most afraid of is messy mixed code. With these alternative syntaxes without braces, all friends who love cleanliness will be so happy that they pee.
2. The process control logic is clearer and the code is easier to read
If you want to change someone else's PHP and HTML mixed code, open it and find out, I will delete it! What a fucking rubbish! If alternative syntax is used, I think no matter how rubbish the program developer is, his writing will not be too messy.
3. Some friends who have transferred from other basic language families such as ASP will find it easier to use PHP.
Talking for a long time is useless, can you get something to do? How to use this thing?
Based on the usage described previously, the alternative syntax for the if statement is as follows:

equal to 5


equal to 5


either 5 or 6


while alternative syntax:

  • What to loop


  • for alternative syntax:

  • What to loop


  • foreach alternative syntax:

  • What to loop


  • switch alternative 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;
    ?>

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895548.htmlTechArticleExample of alternative syntax for PHP process control. PHPers who know the templates of blog programs such as wordpress will see many strange things. PHP syntax, such as: ?php if(empty($GET_['a'])): ? font color=...
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template