PHP IF Statement Tutorial with Stone River eLearning

While working with PHP and displaying content on the webpage, we will come across situations where a different operation must be performed for different cases. For instance, certain social networking websites do not allow users below the age of 13.

Therefore, after the user has entered his/her age in the profile, the webpage must show a message saying “ You are allowed to use this website”; if the age is above 13, similarly, the website must show the message “You are not old enough to use this website”, if the age is below 13. Achieving success with such a problem without the flow control statements can be slow, time consuming and frustrating, both for the user and the programmer.  In this section, we are going to discuss about the ‘if’ statements and the ‘if else’ statements. 

IF Statement

If statement is used when there is a condition to be evaluated and a result to be displayed given that condition holds true. 

Eg:

<?php
	$age=18; //variable definition
	if ($age<20) // condition for the if statement
	{
		echo 'You are not an adult yet'; // If condition holds true    
	}
?>

The given code initialises the value of the variable $age then gives the condition of a string getting printed if the age is less than 20. 

IF-ELSE Statement

Now, there is a very high probability that while designing a webpage or any other PHP program for that matter, we might have to consider the case when a condition applied does not hold true. For instance; when a user is required to enter his age on a webpage, the age entered in this case is bound to be a whole number, but what happens if the user enters an alphabet instead of a number. You can’t just let the webpage crash or not show an error message, as it would make your page look unorganised and unplanned. So what should be done then? Well since IF statement only covers the very basic aspect of a program flow, PHP has the IF-ELSE statement that allows the developer to embed a section for the case when the condition provided in the IF clause does not hold true. 

Eg:-

<?php
	$age = 30; //variable definition
	if ($age<18) //If condition
	{
		echo 'You are not an adult yet'; // If the condition in the ‘IF clause’ holds true
	}
	else // else clause defined
	{
		echo 'Congrats! You are an adult now'; // if condition is false then this statement gets executed

	} 
?>

The example specified above, initialises the variable $age to a value and then gives the condition of a string getting printed if the age is less than 18 and a different string being printed if the value of the variable is more than 18.

IF-ELSE IF Statement

The IF and IF-Else statements covers majority of the problems which have either one or two possible outcomes; however what happens if the number of possible outcomes are more than two. To ensure that our webpage does not show any unexpected error even if the numbers of outcomes are more than two, we must use the IF-ELSE IF statement in our code. The IF-ELSE IF statement works the same way as the IF-ELSE statement with just more than two conditions to deal with. Let us have a look at an example for better understanding. 

Eg:-

<?php
	$day = 'Monday'; //variable declaration
	if ($day == 'Monday') //If condition
	{
		echo 'Today is Monday.';
	} 
	elseif ($day == 'Tuesday') // ELSE IF condition used for a different outcome.
	{
		echo 'Today is Tuesday.';
	}
	elseif ($day == 'Wednesday') // ELSE IF condition used for a different outcome.
	{
		echo 'Today is Wednesday.';
	} 
	elseif ($day == 'Thursday')
	{
		echo 'Today is Thursday.';
	} 
	elseif ($day == 'Friday') 
	{
		echo 'Today is Friday.';
	} 
	elseif ($day == 'Saturday')
	{
		echo 'Today is Saturday.';
	} 
	else
	{
		echo 'Invalid Entry.';
	}
?>

In the above example, there are seven possibilities for the seven days of the week and therefore, if else if statement has been used to solve the problem and derive the desired result.

Note:

Brackets must be used after the completion of the ‘if’ clause and the completion of the else clause. It is important to note that the else keyword remains outside the brackets of the ‘if’ clause.  Following the syntax is important to ensure that the program runs as expected.

While the condition of the ‘if’ statement is specified, it must be noted that the equality operator (‘==’) and not the assignment operator (‘=’); is used for making the comparison in the condition.

Thus, the need for a better and simplified approach to overcome the more realistic and probable hurdles that might occur during the designing of our website, is solved by using the if, if else and if else if flow control statements in PHP. 

Online Technology Courses Stone River eLearning

GET YOUR FREE PHP EBOOK!

Comments