Conditional if else Statements

6 min read

So far, PHP did not bring many advantages over HTML. Everything will change now with the if statement.

With IF, we can check whether certain conditions are met such criteria or not.

The basic skeleton for an if statement in PHP looks like this:

<?php 
 if  (  condition  ) 
     { 
     instructions 
     } 
 ?> 

The condition to be checked is placed in brackets directly after the 'if' introductory. This is followed by the statement to be executed in curly brackets if the condition is fulfilled (true /right).

Let's look at a little example:

<?php
$user = "Wira";
if($user == "Wira")
{
echo "Hello Wira";
}
?>

The IF statement checks if the variable $user has content Wira. If this is true, the condition returns true, and the output is Hello Wira.

It should be noted here that double equals signs are used. With only one equal sign, this if statement would not work, because that would assign the value Wira to the variable. Also, no semicolon (;) may be behind the if statement.

If the variable $user did not have the content Wira, then IF condition would return false, and we would not get any output.

But if we also want to get an output if the variable $user does not have the content Wira, we need the else statement.

The basic structure looks like this:

<?php 
 if  (  condition  )   
     { 
     instructions 
     }   
 else   
     { 
     instructions 
     } 
 ?> 

The else occurs whenever we get a false condition.

For a better understanding here is a simple example:

<?php
$user = "Clause";
if($user == "Wira")
{
echo "Hello Wira";
}
else
{
echo "You are not Wira!";
}
?>

If the variable $user has the content Wira, we get an output Hello Wira, other than that we get as output You are not Wira!.

With this example below, we can have already created a simple, yet secure password query.

<form  action  =  "page2.php"  method = "post" >
<input type = "Password" name = "password" />
<input type = "submit" value = " Submit " />
</form>

In this form, we would enter the password. By clicking on the "Submit" button, the page page2.php is called up, and at the same time, the password is transferred to it.

page2.php must look like this:

<?php
$user = $_POST ["user"] ;

if ( $user != "" )
{
echo "Welcome $user" ;
}
else
{
echo "The field User was not filled out" ;
}
?>

With $password = $_POST ["password"];, assign the content of the password field in the form to the variable.

With the if-statement, it is now queried whether the variable $password has the word secret as content, i.e., whether the word secret has also been entered in the form.

If entered correctly, you will enter the internal (password protected) area. An incorrect entry causes the output "The password is unfortunately wrong."

Of course, we can also create if statements within an if statement. (Nested if)

This looks like this:

<?php
$firstname = "Wira";
$lastname = "Tama";
if($firstname == "Wira")
{
echo "Hello Wira ";
if ($lastname == "Tama")
{
echo "Tama";
}
}
else
{
echo "You are not Wira";
}
?>

First, it checks to see if the variable $firstname has the content Wira. If this is true, then Hello Wira will be displayed.

Then, within the if statement, another if statement is used to check whether the variable $lastname has the content, Tama. If this is also true, Tama will also be output.

If the variable $firstname does not have the content Wira, then Wira will not be outputted. It does not matter if $surname variable is Tama or not.

Similarly, a second if statement may also be in the else part:

<?php
$user = "Wira";
if ($user == "Wira")
{
echo "Hello Wira";
} else
{
if ($user == "Tama") {
echo "Hello Mr. Tama";
} else {
echo "You are not Wira Tama";
}
}
?>

If the variable $user has the value Wira, Hello Wira is returned. If it does not have this content, it checks to see if the value is Tama. If it has this content, then Hello Mr. Tama is issued. If the variable $user has neither the content Wira nor the Tama, then you are not issued Wira Tama.

Furthermore one could solve this also by else if. This is another conditional if statement, if the previous one is not true:

<?php
$user = "Wira";
if ($user == "Wira") {
echo "Hello Wira";
} else if ($user == "Tama") {
echo "Hello Mr. Tama";
} else if ($user == "Syafaatfm") {echo "Hello Mr. Syafaatfm"; }
else {
echo "You are neither Mr. Tama nor Mr. Syafaatfm";
}
?>

You can also query with an if statement if something is not fulfilled. We achieve this with != (Not equal).

A simple example of this:

<?php
$user = "Wira";
if ($user != "Wira")
{echo "You are not Wira"; }
else
{echo "Hello Wira"; }
?>

If the variable $user has a different (Not equal) content than Wira, then you are not outputting to Wira. But if it has this content (Wira), then Hello Wira is displayed.

Although you could also use the query == (double equals), if you want to check if anything has been entered in the form, it is better to use !=.

This would just look like this:

<?php
$user = $_POST ["user"];
if ( $user != "" )
{
echo "Welcome $user" ;
}
else
{
echo "The field User was not filled out" ;
}
?>

First, we get the transferred data from the POST method. If $user is not equal to "" (empty), we receive a welcome $user. If the input field was not filled, The field user was not filled out will be displayed.

When using if statements, it is important to ensure that all curly brackets are closed. Otherwise, the script does not work.

« »