- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
if else statement in PHP with examples
This article is created to cover the three conditional statements used in PHP, which are:
if statement in PHP
The PHP "if" statement is used when we need to execute some blocks of statements (codes) when a specified condition is true. For example:
<?php $num = 10; if($num==10) { echo "The value of \$num is 10"; echo "<BR>"; } ?>
The output produced by the above PHP example is shown in the snapshot given below:
In the preceding example, the condition "$num==10" or "10==10" (as the value of $num has been initialized by 10) evaluates to true.Therefore, the program flow goes inside the if body and executes the two written statements. The first statement prints the value of $num, whereas the second statement inserts a line break using the BR tag.
The above program can also be written in this way:
<?php $num = 10; if($num==10) echo "The value of \$num is 10<BR>"; ?>
Since there is a single statement available as the body of if, there is no need to put the statement inside curly braces or {}. A single statement does not require that you write inside a curly brace. However, you can use curly braces for single statements too.
The syntax of the if statement in PHP is:
if(condition)
{
// block of code to execute if condition is true
}
if...else statement in PHP
The PHP if...else block is used when we need to execute some block of code when the specified condition evaluates to be true and some other block of code when the specified condition evaluates to be false. For example:
<?php $num = 20; if($num==10) echo "The value of \$num is 10"; else echo "The value of \$num is not 10"; ?>
The output of this PHP example should be:
The value of $num is not 10
Since the condition $num==10 or 20==10 evaluates to be false, the program flow goes inside the else body, and the statement available inside the else body gets executed.
That is, the program executes the block of code available in the body of if when the condition evaluates to be true. Otherwise, the program executes the block of code available in the body of the counterpart of if, that is, else, when the condition evaluates to be false.
The syntax of the if...else statement in PHP is:
if(condition) { // block of code to execute if condition is true } else { // block of code to execute if condition is false }
if...elseif...else statement in PHP
The PHP if...elseif...else statement is used when we need to execute a particular block of code based on multiple conditions. For example:
<?php $num = 0; if($num<0) echo "The value of \$num is a negative number."; elseif($num==0) echo "The value of \$num is 0."; else echo "The value of \$num is a positive number."; ?>
The output of this PHP example should be:
The value of $num is 0.
The syntax of the if...elseif...else statement in PHP is:
if(condition_1) { // block of code to execute, if condition_1 is true } elseif(condition_2) { // block of code to execute, if condition_2 is true } elseif(condition_3) { // block of code to execute, if condition_3 is true } elseif(condition_4) { // block of code to execute, if condition_4 is true } . . . elseif(condition_N) { // block of code to execute, if condition_N is true } else { // block of code to execute, if all conditions are false }
If multiple conditions evaluate to be true, for example, let's suppose that condition_2 and condition_3, both evaluate to be true. In that case, the first condition that evaluates to true is executed, and the rest is skipped. For example:
<?php $x = -10; if($x > -10) echo "The value of \$x is greater than -10"; elseif($x == -10) echo "The value of \$x is -10"; elseif($x < 0) echo "The value of \$x is less than 0"; elseif($x == 0) echo "The value of \$x is 0"; elseif($x > 0) echo "The value of \$x is greater than 0"; else echo "The value of \$x is unknown"; ?>
Since the conditions $x == -10 and $x < 0 are both true, but first the condition $x == -10 has been evaluated, the block of code available inside that block will be executed, and the rest of the execution of the same if...elseif...else structure gets skipped. Therefore, the output should be:
The value of $x is -10
Advantages and disadvantages of the if, if...else, and if...elseif...else statements in PHP
if | if...else | if...elseif...else | |
---|---|---|---|
Advantage | The "if" statement is used to test a single condition; if the condition evaluates to true, the code contained within the if block is executed. Its benefit is that it permits simple branching on the basis of a single condition. | When there are two possible outcomes based on a condition, "if...then" is used. The code within the "if" block is executed if the condition is true; otherwise, the code within the "else" block is executed. Its benefit is that it offers two possible execution paths based on a single condition. | When there are several potential outcomes depending on various conditions, the "if...elseif...else" statement is used. Multiple conditions may be tested, and depending on the first one that evaluates to true, the code block contained within the corresponding "elseif" or "else" block is executed. Its benefit is that it gives you a way to test multiple assumptions and execute the relevant code block based on the first assumption that is true. |
Disadvantage | The "if" statement is the most fundamental conditional statement in PHP and can only check a single condition. It lacks a fallback option if the condition is not met, which can result in incomplete logic. | The "if...else" statement is superior to the "if" statement because it offers an alternative if the condition is not met. However, it only permits the checking of two conditions and can become verbose and difficult to read if multiple conditions must be checked. | The "if...elseif...else" statement is the most complex conditional statement and permits the evaluation of multiple conditions. However, if there are too many conditions, it can become difficult to maintain and overly complicated. In addition, it may not be the most efficient solution in certain circumstances because it evaluates all the conditions until it finds a match, even if a match is discovered early on. |
« Previous Tutorial Next Tutorial »