How to write if else in HTML in PHP?

The PHP if statement will execute one block of code between two or many options – so taking the decision based at certain given criteria.

Syntax of PHP if statement

If there are only two options then you may use simply if with the else statement. If the expression in small brackets is true, it will execute the block inside the if statement, otherwise, the else block of code will execute.

1

2

3

4

5

6

7

8

9

if ( $Number == 3 ) {

 

echo "Display Something, if the Number is 3";

 

} else {

 

echo "For any other number, or the condition is false!";

 

}


Note: You may also use only if without the else statement

If options are more then use if..elseif with else statements. For example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

if ($Month == "Jan")

 

 

 

echo "Execute Jan code";

 

 

 

elseif ($Month == "Feb")

 

 

 

echo "Execute Feb code";

 

 

 

elseif ($Month == "Mar")

 

 

 

echo "Execute March code";

 

 

 

else

 

 

 

echo "Any other month";


 

Note: If you have multiple lines to execute, enclose each block in curly brackets.

I will explain both with visual examples in this tutorial, so you may understand how to use if..else with HTML or other web elements, as such PHP is generally used for web based projects.

A simple example to start with – the if..else

Before going to user selection based execution of a block in the if..else statement, in this example, I will simply use a static value to show how it works, to give an idea if you are a beginner.

In the example, I have simply declared a variable and assigned it a numeric value.

How to write if else in HTML in PHP?

See online demo and code

Only, the if..else statement is used in the example. If the given number is 3, it will be evaluated as true in the if statement, so the statement inside the if statement will execute. If it is some other number the PHP else statement part will execute. You may copy the code and try it in your PHP editor.

The PHP elseif statement with select dropdown, div, and CSS

Following is an example to use if with elseif PHP statement. As you open the demo page, an HTML dropdown will be displayed. In the drop-down, you can select a color.

On the change event of dropdown, the same PHP file is called where I have used if with the elseif statement.

As you select a color from the dropdown, a div with the same color will be displayed.

First have a look at the demo followed by a little description:

How to write if else in HTML in PHP?

See online demo and code

In the example, following block of codes are used in the if and elseif statements of PHP:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<?php

 

$Color = $_REQUEST["color"];

 

if ($Color == "red"){

 

echo "<div style='width:40%;height:300px;background-color:red;'></div>";

 

}

 

elseif ($Color == "orange"){

 

echo "<div style='width:40%;height:300px;background-color:orange;'></div>";

 

}

 

elseif ($Color == "yellow"){

 

echo "<div style='width:40%;height:300px;background-color:yellow;'></div>";

 

}

 

elseif ($Color == "green"){

 

echo "<div style='width:40%;height:300px;background-color:green;'></div>";

 

}

 

elseif ($Color == "maroon"){

 

echo "<div style='width:40%;height:300px;background-color:maroon;'></div>";

 

}

 

elseif ($Color == "blue"){

 

echo "<div style='width:40%;height:300px;background-color:blue;'></div>";

 

}

 

elseif ($Color == "pink"){

 

echo "<div style='width:40%;height:300px;background-color:pink;'></div>";

 

}

 

else{

 

echo ("Select a Color from dropdown");

 

}

 

?>


 

You can see, first the chosen color is picked by using the $_REQUEST[“color”]; and assigned to the $color variable. After that, an if statement is used where the value of the color is checked. If the value is “red” it will display the div of red color by using the echo statement.

If orange was selected in the dropdown, the elseif statement after the if statement will be evaluated as true. So the div with the orange color is displayed. If some other color is chosen from the dropdown, it will keep on checking the elseif statements.

Following is the code of HTML for dropdown Where the PHP file is called with the selected color:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<p>

 

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

 

<option>Select a Color</option>

 

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

 

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

 

<option value="yellow">Yellow</option>

 

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

 

<option value="maroon">Maroon</option>

 

<option value="blue">Bluish</option>

 

<option value="pink">Pink</option>

 

</select>

 

</p>


 

Note that, this example is just to demonstrate the use of if..elseif statements. In real time, you may use JavaScript to change the color of div or some other elements in web page.

A PHP if else example to check password format

In this example of the if..else statement, a user is asked to enter the email address in a textbox. After entering the email and pressing the submit button, the PHP script will check the format of email address and displays the message accordingly.

See the demo online:

How to write if else in HTML in PHP?

See online demo and code

As you submit the button, the textbox value is assigned to $address variable by using the $_POST[‘address’]. This variable is checked in the PHP if else condition to validate for the correct email format by using a built-in function, FILTER_VALIDATE_EMAIL:

1

2

3

4

5

6

7

8

9

if(filter_var($address,FILTER_VALIDATE_EMAIL)){

 

echo "$address email is valid!";

 

}else{

 

echo "$address is an invalid email!";

 

}


 

If the entered email is correct, the if condition will be true and echo statement will display the given string. Otherwise, the else statement will execute the given statement.

An example of if..else with foreach loop and arrays

In this example, I will use the if statement with a foreach loop and arrays. For that, a simple numeric array is created with five elements.

The elements are: 5, 10, 15, 20, 25. After that, a foreach loop is used to display those array elements.

The task is to quit the foreach loop as it reaches the value of 15 or more.

How to write if else in HTML in PHP?

See online demo and code

To accomplish that, I simply used an if statement. In the if statement following condition is given:

1

2

3

4

5

if ($i >=15 ){

 

break;

 

}


 

So as soon as the value of variable $i reached 15 (which is the value of current array element), the break statement will stop the loop and control will move to the next line outside of the foreach loop.

The complete code is given in the demo page.

An example of nested if statement

In certain scenarios, you may need to check two or more conditions after the first condition is true. In that case, you should perform some action if the first condition is true and then check the second condition by using another if..else inside the first if statement.

Using another if statement inside the first if statement is nested if. See the following structure:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

if ($a == 10){

 

echo ("The value is 10");

 

if($b == 20){

 

echo ("The value of b is 20");

 

}

 

}

 

//closing first if statemnt

 

else {

 

echo ("The value of a is not 10");

 

}


 

So inside the curly brackets of first if statement, another if statement is used before you close it. This will start nested if where you can check another condition. You may go further to use another nested if inside second if statement and so on.

After using the if statement (with else or elseif) inside the first if, close the curly bracket. There you can use elseif or else that will relate to the first if statement.

Conclusion:

The if..else, elseif statements in PHP are used to make decisions based at some criteria. You have to give an expression in the if statement which is evaluated. If it returns as true, it will perform the given actions otherwise, it will move to the next elseif statement (if provided). If all conditions are false, the else part will execute and after that control will move to the next line of code outside of the if statement.

You may use comparison operators like ==, !=, >, <, <= etc. and logical operators like and, or, &&, || etc. to evaluate a condition.

How to use if and else in HTML?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

Can we write if condition in HTML?

A conditional statement begins with an "if" statement to determine if an action should be taken. Conditional statements can be summarized as "if this then that." To review the basics of conditional statements see Advanced Merge Tags and Conditional Statements.

Can I use an if statement in PHP?

You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false.

Can HTML be embedded inside PHP if statement?

Yes, HTML can be embedded inside an 'if' statement with the help of PHP.