A detailed example of "If...else" statement in PHP control statement

Source: Internet
Author: User
Before you introduce the "if" statement, know that the "if" statement is to meet the expression when the execution of a PHP statement, when the condition is not satisfied, is not executed, but, in most cases, always meet the conditions when the execution of a PHP statement, when the condition is not satisfied with the execution of other PHP statements, At this point, we need to use the information we present to you today "If...else" statementThe

Let's take a look at the syntax format of the "If...else" statement

if (conditional expression) {            PHP statement 1;} else{            PHP statement 2;}

The statement is detailed:

The above statement means: When our conditional expression is true, execute PHP statement 1, and if the conditional expression is false, execute PHP statement 2. This satisfies the PHP statement execution when we satisfy the condition of the expression, and the PHP statement executes when the expression condition is not met.

Let's take a look at the Process Control chart for the "If...else" statement


If...else Statement Instance

This example will be based on an example of an if statement, and an example of an if statement will have the result output only if the expression is true, otherwise there will be no output. But when we use the If...else statement, regardless of the full expression condition, the output will result, the code is as follows

<?phpheader ("Content-type:text/html;charset=utf-8"), $num =rand (1,50), if ($num%2==0) {    echo "variable $num is even";} else{    echo "variable $num is odd";}? >

Example Explanation:

The rand () function is to get a random integer.

First use the rand () function to generate a number between 1,50, and then the condition to determine whether the number is even, if it is even, the conditional expression is true, then the output "variable $num is even", anyway, the conditional expression is false, the output "variable $num is odd",

The result of the code operation is as follows:

Because it is a randomly generated number, each time you refresh the page, there will be different results. Try it!

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.