PHP IF ELSE STATEMENT

In php, there may lots of situation rise where we need to check whether the condition is matching, if condition matches, then execute certain task otherwise don’t execute task.

IF STATEMENT

If statement is useful to check if a single condition matches, then execute a task.

Syntax:

if(condition){

}

Note: Condition upon which, if statement depends, that is put inside brackets (Parenthesis) and the task which we want to execute if condition met, that is kept inside the curly braces “{}”.

Example:

Lets take another simple example:

I have two variables and I need to check if first variable value is larger than second variable value, if yes, then print a message about it.

<?php

$num1=10;

$num2=5;

if($num1>$num2){

echo “First variable value is larger than second variable value”;

}

?>

The above code would print “First variable value is larger than second variable value”

Example:

Suppose a company has a website, which at the back end, one of the PHP Script checks whether today is midnight of 1st day of a month, if yes, then transfers salary to employee.

Here the condition is to check if today is midnight of 1st day. If yes, then issue salary. Following is the syntax.

if(today==1st day of month){

issue_salary();

}

In the above syntax, you could notice, issue_salary();

It is a function. We are going to cover about this function in another chapter. But for now, remember, a function executes certain task. issue_salary() function issues salary to employees. (Just keep the above syntax picture in mind, once we cover function, we would dig deep to the required php code.)

IF ELSE STATEMENT:

If statement is not sufficient to execute task. There may be situation, what if condition does not match. We need to execute certain other task. In such scenario, if else statement is useful.

Syntax:

if(condition){

}else{

}

The above syntax has two blocks:

  1. if condition block ( if(condition){} )
  2. else block (else{})

We use if else statement because if a condition met, execute certain task otherwise execute other task. So, all the codes required to execute a task if a condition met, goes inside if block. And all the codes required to execute task if condition did not meet goes inside else block.

Example:

We have two variables: $num1=1000, $num2=2000. Write a PHP code to print which is greater. 

<?php

$num1=1000;

$num2=2000;

if($num1>$num2){

echo “num1 is greater than num2”;

}else{

echo “num2 is greater than num1”;

}

?>

 

The output would be: num2 is greater than num1.

IF ELSE IF ELSE STATEMENT

In other situation, we may land up where we may face multiple conditions. In such situation, if else statement is not sufficient to handle the requirement. We need if…else if…else statement.

Syntax:

if(condition 1){

//execute task 1

}else if(condition 2){

//execute task 2

}else if(condition 3){

//execute task 3

}else{

//execute tak 4

}

In the above code, observer how else if condition is written. first comes else, then a single space then if condition. If you miss this single space, then it will throw error.

The above syntax would become clear from following example:

Example:

Suppose a college has a website, which instructs the canteen to prepare meal for students for the entire week.(Saturday and Sunday : College is closed. )For this week, the meal schedule is as below

Monday: Aloo Parantha

Tuesday: Lemon Rice

Wednesday: Pizza

Thursday: Noodles

Friday: Cake

Therefore, the php code to print message to the canteen could be as below:

<?php

$today=”Wednesday”;

if($today==”Monday”){

echo “Aloo Parantha”;

}else if($today==”Tuesday”){

echo “Lemon Rice”;

}else if($today==”Wednesday”){

echo “Pizza”;

}else if($today==”Thursday”){

echo “Noodles”;

}else if($today==”Friday”){

echo “Cake”;

}else{

echo “today is holiday”;

}

 

?>

Note: In the above code, I have defined the day of the week. It is Wednesday and assigned to today variable. ($today=”Wednesday”)

The rest of the code is self explanatory. This way we write php if else if else statement. Use if else statement as per need.