PHP conditional control statement instance program

Source: Internet
Author: User
Tags php 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: Copy code

<? Php

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: Copy code

<? Php
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: Copy code

<? Php

$ 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: Copy code

<? Php

$ 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 & prime;: 'execution statement 2 & prime ;;

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: Copy code

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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.