If Statement in PHP

In this tutorial, we are going to cover the concept of the if statement and will also see the way to use a select option with an if statement in PHP, let’s begin the tutorial with an introduction first.


An if statement is a technique to check whether a running condition is working in a proper way or not. In simple words, we can say the if statement is used whenever we want to check the availability or execution of a particular function in a program.  

A block of code inside the if statement executes only when a declared condition gets true, in case the condition becomes false so the Else part of the if statement executes.

Syntax 1

if (condition){
//Block of code will be executed if the condition results true
//Block of code will be executed if the condition results false

The if statement is a very convenient way to verify the programs, the best thing about the if statement is you can check multiple conditions at the same time by just adding an extra block of code of the else if statement. 

Syntax 2

if (condition){
//Block of code will execute if the condition results true
 else if(){
//Block of code will execute if the previous condition results false.
//Block of code will execute if previous conditions result false

Let’s try some examples for better understanding.

Example 1

$num= "2";
if ($num=="2") {
echo "number 2";
echo "not found";

Code Explanation

In the above code, we declared a variable and assigned it an integer value “3” then we defined the if condition,

That if the declared variable is equal to 3, so a block of code will execute that will echo “number 2”.

if the condition is not true then the else statement will print a “not found” message. Check out the results below.


number 2

Example 2


$year= "2023";

if ($year == "2022") {
echo "year is 2022";
else if ($year == "2023") {

           echo "year is 2023";

echo "year not declared";

Code Explanation

In the above code, we checked multiple conditions, first of all, we declared a variable and then defined an if condition, to check another condition we used the else if statement that will execute when the previous if condition will become false. , in the end, we used the else statement that will execute when both previous conditions will result false, see the output below.


year is 2023

let’s move forward now and see how to use the select option with the if condition, check out the example below.

How to use the select option with the if condition

suppose you are working on an application where a user can customize anything by himself or herself by keeping in mind this functionality you are building logic that when a user selects an option the desired effect should apply.

But the confusion is that how would the program know that which option is being selected and which effect should be applied. 

So for this purpose, we will use the select option with if conditions to check the selected option.


<select onChange="window.location='myyy.php?color='+this.value">

<option>Select a Color for Cirlce</option>

<option value="Black">Black</option>

<option value="Blue">Blue</option>

<option value="Red">Red</option>

<option value="Orange">Orange</option>

<option value="Green">Green</option>




$change_Color = $_REQUEST["color"];

if ($change_Color == "Black"){

echo "<div style='width:15%;height:200px;background-color:black; border:solid; border-radius:120px;margin:auto;'></div>";


elseif ($change_Color == "Blue"){

echo "<div style='width:15%;height:200px;background-color:blue; border:solid; border-radius:120px;margin:auto;'></div>";

elseif ($change_Color == "Red"){

echo "<div style='width:15%;height:200px;background-color:red; border:solid; border-radius:120px;margin:auto;'></div>"; 
elseif ($change_Color == "Orange"){

echo "<div style='width:15%;height:200px;background-color:orange; border:solid; border-radius:120px;margin:auto;'></div>";
elseif ($change_Color == "Green"){

echo "<div style='width:15%;height:200px;background-color:green; border:solid; border-radius:120px;margin:auto;'></div>";
echo "<div style='width:15%;height:200px;background-color:white; border:solid; border-radius:120px;margin:auto;'></div>";

Code Explanation

In the above code, we used a select tag and defined a few options inside it then we declared if and else if statements, each statement will check each option that which option is selected by the user when a condition becomes true so a block of code will be executed for that particular option.

Basically, the above program will make a user select a desired color from the drop down menu and that selected color will be applied to a circle.

Check out the results below.


As you can see in the above output the selected color has been applied to the circle.


In this tutorial, we talked about the if statements and the select option with if statements. First of all, we saw a brief introduction to if statements in PHP and then we discussed a few examples by using if, else if statements.

In the end used select option with if, else if conditions as well by taking a hand on an example.

That’s all for today hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Leave a Comment