php conditional statements

Very often you need to perform different actions on different conditions. You can use conditional statements to do so in PHP. In PHP we have the following conditional statements.

if statement - executes if one condition is true.

if...else statement - executes if a condition is true and another code in case the condition is false.

if...elseif....else statement - executes different codes for more than two conditions.

switch statement - selects one of many blocks of code to be executed

IF STATEMENT

Syntax

if (condition) {
	code to be executed if condition is true;
}

Example

$t = date("H");
if ($t < "20") {
	echo "Have a good day!";
}

The if...else Statement

Syntax

if (condition) {
	code to be executed if condition is true;
} else {
	code to be executed if condition is false;
}  

Example

$t = date("H");
if ($t < "20") {
	echo "Have a good day!";
} else {
	echo "Have a good night!";
}

Example

Without using AND

$a = 10;
$b = 20;
$c = 30;
if($a > $b){
	if($a > c){
		echo 'A is greater' . $a;
	}else{
		echo 'C is greater' . $c;
	}
}else{
	if($b > $c){
		echo 'B is greater' .$b;
	}else{
		echo 'C is greater' .$c;
	}
}

It's always great to take care of the indentation as it makes your code easily readable.

ELSEIF

elseif condition supports multiple if statements. it acts as a group for multiple if statements and prevents the code from running multiple codes for a single output.

Syntax

if (condition) {
	code to be executed if this condition is true;
} elseif (condition) {
	code to be executed if this condition is true;
} else {
	code to be executed if all conditions are false;
}

Example

$a = 10;
if($a == 10){
	echo 'A is 10';
}
if($a == 20){
	echo 'A is 20';
}
if($a == 30){
	echo 'A is 30';
}

Now, the same block of codes with ifelseif statement.

Example

if($a == 10){
	echo 'A is 10';
}elseif($a == 20){
	echo 'A is 20';
}elseif($a == 30){
	echo 'A is 30';
}else{
	echo 'Unknown Value';
}

In cases where multiple if statements have true conditions.

Example

$a = 30; 
if($a > 10){
	echo 'A is 10';
}
if($a > 20){
	echo 'A is 20';
}
if($a > 30){
	echo 'A is 30';
}

Result: A is 10. A is 20

Example

if($a > 10){
	echo 'A is 10';
}elseif($a > 20){
	echo 'A is 20';
}elseif($a > 30){
	echo 'A is 30';
}else{
	echo 'Unknown Value';
}

Result: A is 10.

Switch Statement

Syntax

switch (n) {
	case label1:
		code to be executed if n=label1;
	break;
	case label2:
		code to be executed if n=label2;
	break;
	case label3:
		code to be executed if n=label3;
	break;
	...
	default:
		code to be executed if n is different from all labels;
}  

Example

$a = 30;
switch ($a){
	case 10:{
		echo 'A is 10';
	}
	case 20:{
		echo 'A is 20';
	}
	default:{
		echo 'Unknown Value';
	} 
}

default acts similar to else in if statement.

Result: Unknown Value

Example

$a = 10;
switch ($a){
	case 10:{
		echo 'A is 10';
	}
	case 20:{
		echo 'A is 20';
	}
	default:{
		echo 'Unknown Value';
	} 
}

Result: A is 10 A is 20 Unknown value.

If any condition is true in a switch statement, in such cases, the statement assumes all the preceeding conditions are also true. So, we need to stop the code from executing further statements once a condition is true and for that we use break. With break we can remove those curly braces as well.

Example

$a = 10;
switch ($a){
	case 10:
		echo 'A is 10';
		break;	
	case 20:
		echo 'A is 20';
		break;	
	default:
		echo 'Unknown Value';
	
}

0 Like 0 Dislike 0 Comment Share

Leave a comment