PHP Lesson 9 – Conditional if, if else statements

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

if elsestatement

So far, PHP has not had much of an advantage over HTML. This will change now with the if statement.

With if we can check whether certain conditions are met or not met.

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

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

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

Let’s look at a little example:

<?php
$user = "Coding180";

if($user == "Coding180") 
   {
   echo "Hallo Coding180";
   }
?>

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

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

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

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

The basic if statement syntax in PHP is:

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

The else occurs whenever we get false .

For a better understanding here is a small example:

<?php
$user = "coding180";

if($user == "robort") 
   {
   echo "Hello robort";
   } 
else 
   {
   echo "you are not robort!";
   }
?>

If the variable $user has the content Coding180, we get as output Hello Robort, with every other content we get as output You are not Robort! ,

Hereby we can already create a simple, yet secure password query.

First, we need a form again:

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

In this form, we enter the password. By clicking on “Submit” the page page2.php is called up and at the same time, the password is transferred to it.
– Just as described in the section Passing Variables under Variables.

page2.php must look like this:

<?Php
$password = $_POST["password"];
 
if ($password == 'secret')
   {
   echo "Welcome to the internal area";
   }
else
   {
   echo "The password is wrong";
   }
?>

With $password = $_POST[“password”]; assign the content of the password field in the form to the variable.
The if-statement now queries whether the variable $password has the word secret as content, that is, 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 wrong.

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

This looks like this:

<? Php
$firstname = "Robort";
$surname = "Gabriel";


if ($firstname == "Robort")
   {
   echo "Hello Robort";

   if ($lastname == "Gabriel")
      {
      echo "Gabriel";
      }
   }
else
   {
   echo "You are not Robort";
   }
?>

First, it checks to see if the variable $firstname has the content Robort. If this is true, then Hello Robort will be printed out.
Then, within the if statement, another if statement is used to check whether the variable $surname has the content, Gabriel. If this is also true, Gabriel will also be output.

If the variable $firstname does not have the content Robort, then you are not outputted Robort.
It does not matter if $surname is Gabriel or not.

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

<? Php
$user = "Robort";

if ($ user == "Robort")
   {
   echo "Hello Robort";
   }
else
   {
   if ($ user == "Gabriel")
      {
      echo "Hello Mr. Gabriel";
      }
   else
      {
      echo "You are not Robort Gabriel";
      }
   }
?>

If the variable $user has the content RobortHello Robort is output. If it does not have this content, it checks if the content is Gabriel. If it has this content, then Hello Mr. Gabriel will be issued. If the variable $user has neither the content Robort nor the content Gabriel, then you are not outputting to Robort Gabriel.

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

<?Php
$user = "Robort";

if ($user == "Robort")
   {
   echo "Hello Robort";
   }
else if ($user == "Gabriel")
   {
   echo "Hello Mr. Gabriel";
   }
else if ($user == "coding180")
   {
   echo "Hello Mr. coding180";
   }
else
   {
   echo "You are neither Mr. Gabriel nor Mr. coding180";
   }

?>

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

A small example of this:

<?php
$user = "Coding180";

if($user != "Coding180")
   {
   echo "this is not Coding180";
   }
else
   {
   echo "Hello Coding180";
   }
?>

If the variable $user has a different (unequal) content than Coding180, then you are not output to Coding180.
But if she has this content ( Coding180 ), then Hello Coding180 will be issued.

Although you could also use the query == (actual-equal), but 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 User field was not filled out";
   }
?>

 

About The Author

Related posts

Leave a Reply