PHP if..else

PHP supports the following types of if..else statement

  • if statement
  • if...else statement
  • if...else if... statement

PHP if statement

The if statement is the basic control statement that check the condition if condition is true then block of statement to be execute.

Syntax

if (condition) {
   Statement(s) to be executed if condition is true.
}

Example

$check = 5;
if($check>4)
{
 echo 'eligible for gratuity';
}
//Output
//eligible for gratuity

PHP if...else statement

The if...else statement(s) executes some code if a condition is true and another code if that condition is false.

Syntax

if (condition) {
   Statement(s) to be executed if condition is true.
} else{
	Statement(s) to be executed if condition is false.
}

Example

$check = 18;
if($check>17)
{
 echo('eligible for vote');
} else{
 echo('Not eligible for vote');
}
//Output
//if var check value is 18 then eligible for vote
//if var check value is 18 then Not eligible for vote

PHP if...elseif...else Statement

The if...else if... statement used for more than two conditions to be checked

Syntax

if (condition) {
   Statement(s) to be executed if condition is true.
else if (condition) {
   Statement(s) to be executed if condition is true.
} else{
   Statement(s) to be executed if no condition is true.
}