PHP if Statement Example Tutorials

By | 2018-04-01T10:46:26+05:30 January 23rd, 2017|PHP|

PHP conditional statements are used in various conditions that require decision-making. A fundamental statement is the PHP if statement.

In PHP if statement is further branched into the following.,

PHP if statements :

PHP if is used, when validating a certain parameter and prompting a decision to be made.

Syntax :


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

Example :


<?php

$a = 10;

if ($a > 5) {
global $a;
echo "The apples are $a";
}
?>

Output:

The apples are 10

PHP If…else statement :

The if…else statement is used to give more than one option of execution if one condition is false and the other is true.

Syntax :


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

Example :


<?php

$a = 1;

if ($a > 5) {
global $a;
echo "The apples are $a";
} else{
echo "The apples are less then 5";
}
?>

Output:

The apples are lessthen 5

PHP If— elsif… else statement :

PHP If— elsif… else statement executes different codes for more than two conditions.

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 :


<?php

$a = 100;

if ($a > 5 && $a < 20) {
global $a;
echo "The apples are $a";
} elseif ($a < 5) {
echo "The apples are less than 5";
} else {
echo "There are too many apples to count in a second";
}
?>

Output:

There are too many apples to count in a second

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment