PHP Conditional Statements
PHP Conditional Statements are check condition and perform the action
Following condition, statements use in PHP
Following condition, statements use in PHP
- if statement
- if else statement
- if elseif else statement
- switch case statement
if statement
if statement executes code when condition true. It cannot execute code when the condition False.
Syntax
if (condition)
{
execut code if condition is true;
}
Example:-
<?php
$t = date("H");
if ($t < "20")
{
echo "Have a good day!";
}
?>
Output:-Have a good day!
if else statement
if statement executes code when condition true otherwise else statement. when condition false execute else statement.
Syntax
if (condition)
{
execut code if condition is true;
}
else
{
execut code if condition is false;
}
Example:-
<?php
$a = 30;
$b = 20;
if ($a <$b)
{
echo "a is greater than b";
}
else
{
echo "b is less than a";
}
?>
if elseif else statement
if elseif else statement use multiple if else statement.
Syntax
if (condition)
{
execut code if condition is true;
}
elseif (condition)
{
execut code elseif condition is true and if condition is false;
}
else
{
execut code all condition is false;
}
<?php
$a = 30;
$b = 20;
if ($a <$b)
{
echo "a is greater than b";
}
elseif ($a =$b)
{
echo "a is equal to b";
}
else
{
echo "b is less than a";
}
?>
switch case statement read here
PHP if-else elseif | PHP Conditional Statements | PHP Tutorial
4/
5
Oleh
Kavi