If…Else Statements in PHP

Conditional Statements is using to, execute some code if a condition is true and another code if the condition is false.

Syntax (Single Line):
if (condition) Program will executed if condition is true;

Syntax (with else condition):
if (condition)
Program will execute if condition is true;
else
Program will execute if condition is false;

Syntax (with elseif and else condition):
if (first condition)
Program will execute if first condition is true;
elseif (second condition)
Program will execute if second condition is true;
else
Program will execute if all condition is false;

Syntax (with elseif and else condition separated using Curly Brackets):
if (first condition) {
Program will execute if first condition is true;
} elseif (second condition) {
Program will execute if second condition is true;
} else {
Program will execute if all condition is false;
}

Compare value of two variable $a and $b using PHP
<?php
$a = 8;
$b = 4;
if($a==$b} {
    echo “The value of a and b are same”;
} else {
     echo “The value of a and b are not same.”
}
?>

Output : The value of a and b are not same.

Ternary operator :

In addition to the traditional syntax for if (condition) action ie,
ternary operator that syntax is,

(condition ? action_if_true: action_if_false;)

Compare value of two variable $a and $b using Ternary operator in PHP
<?php
$a = 8;
$b = 4;
echo ($a==$b)?”Logical condition is true.” : “Logical condition is false”;
?>

Output : Logical condition is false.