PHP Conditional Control statement instance Program-PHP source code

Source: Internet
Author: User
In php, the conditional control language includes the if statement, swicth statement, and three non-operators. Below we will summarize some of the three Application Instances in php, I hope this tutorial will be helpful to all of you. In php, the conditional control language includes the if statement, swicth statement, and three non-operators. Below we will summarize some of the three Application Instances in php, I hope this tutorial will be helpful to all of you.

Script ec (2); script

There are three PHP conditional control statements:

First: if statement;

Type 2: switch statement;

Third: ternary operators;

The three condition control statements are described respectively.

First: if statement.


The if syntax has the following formats:

1 ),

If (expression 1) {// The following program segment is executed to meet the conditions

Execute the statement;
}

For example:

The Code is as follows:

If ($ a> $ B ){
Echo "a is bigger than B ";
$ B = $;
}
?>

2 ),

If (expression 1) {// The following program segment is executed to meet the conditions

Execute the statement;

}

Else {// does not meet the following procedure section

Execute the statement;

}

For example:

The Code is as follows:

If ($ a> $ B ){
Echo "a is bigger than B ";
} Else {
Echo "a is NOT bigger than B ";
}
?>

3), if (expression 1) {// The following program segment is executed to meet the conditions

...

} Elseif (expression 2 ){

...

} Else {

...

}

For example:

The Code is as follows:

$ A = 45;

$ B = 50;

If ($ a> $ B) {print "a is greater than B ";}

Elseif ($ a = $ B) {print "a is equal to B ";}

Else {print "a less than B ";}

?>

Type 2: switch statement.

A Switch is a multi-path branch statement. Although multiple nested if statements can be used to implement multiple branches, the deeper the nesting, the worse the program readability. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else.

Switch statement syntax

Switch (var ){
Case value 1:
Statement 1;
Break;
Case value 2:

Statement 2;
Break;
Case value N:

Statement N;
Break;
Default:

Statement;

}

For example:

The Code is as follows:

$ I = 1;

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 no equals ";
Break;
}
?>

Third: ternary operators.

PHP script language support operator? :, Which is a compact version of the if statement. We can use the following example to explain the ternary OPERATOR:


Ternary operator Syntax:

(Condition )? 'Execution statement 1': 'execution statement 2 ′;

Explanation: if the condition is TRUE, Statement 1 is executed. If the condition is FALSE, Statement 2 is executed;

For example:

The Code is as follows:

Echo ($ a> $ B )? "A is greater than B": "a is not greater than B ";

Explanation:

If $ a> $ B, enter a greater than B. If $ a <$ B, enter a not greater than B.

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.