PHP

PHP if Statement Example Tutorials

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 :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Python

How to use Python Ternary Operator

In this article, we are going to discuss how to use python ternary operator and implementing nested-if-else with ternary operator along with basic examples Ternary Operators are also…

Ask a question or leave a comment