In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition.
There are four types of conditional statements in PHP
- If statement
- If-else statement
- If-elseif-else statement
- Switch statement
Let us explain you each of the above with simple examples.
- IF-statement
Executes the block of code if the specified condition is true.
Syntax :
If(condition)
{
code;
}
- If-else statement
If the condition in the if statement is found false then the code in else block is executed.
Syntax:
if(condition)
{
code;
}
else;
{
code;
}
- If-elseif-else statement
Is used when more than one block of code needs to be checked and executed.
Syntax:
if (condition)
{
code;
}
elseif (condition)
{
code;
}
else
{
code;
}
Let us show you a best example on all the three control statements which can give a intelligence to your website or application.
Example:
<html>
<body>
<h1> Simple welcome note </h1>
<?php
$d=date(“D”);
if ($d==”Fri”)
echo “Have a nice weekend!”;
elseif ($d==”Sun”)
echo “Have a nice Sunday!”;
else
echo “Have a nice day!”;
?>
</body>
</html>
Also see
- PHP Tutorial 1: Introduction for Beginners
- PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility
- PHP Tutorial 3: Variables in PHP
The Geeks Club Apple, iOS, Android, Smart phones, Social Media and the Internet

