Learn Coding #3 - PHP - if - else - elseif

By @smooms12/12/2017tutorial

LearnCoding3.jpg

You often have cases where you want to execute php code if certain conditions are given. And other code if it's not. To implement this you use if , else and elseif.

How to use


Like stated in the last tutorial there are different types of variables. Today we need the boolean (The value can either be true or false)

$a = 5;
$b = 6;
$c = $a < $b; // $c = true
$c = $a > $b // $c = false

To clarify how an if statement is built:

$username = "smooms";
$enteredPassword = "password";

$databasePassword = getUserPassword($username);
// getUserPassword() would be a function call which will return the password of given username
// not realy realistic but it's enough for example purpouses
// how functions work will be shown in later episodes

if ($enteredPassword == $databasePassword) {
    // code to execute if password is correct
} else {
   // code to execute if password is wrong
}

So this is a small example how an if-else could look like. You will see that I used 2x =. In an if statement with 2 = the 2 values will be checked. If they are equal the statement returns true.
You can also use 3x =, then the datatype will also be checked. In our example, $enteredPassword has to be from the same type as $databasePassword (if 3 = where used)

Another ways to use if-else


If there is not much code involved in an if statement you can also do one liners.
Example:

$a = 5;
$b = 6;
$c = ($a < $b) ? $a : $b; // if true then $c = $a else $c = $b

Syntax: (condition) ? [what do if true] : [what do if false];

I often use this example inside HTML Forms. For autofilling fields. If you have a Login form it would be nice that if you enter your credentials wrong that your username keeps disappearing each time you submit the form. (I will show you in another tutorial how this works)

Another good example how to use if-else inside of HTML Code is a user contribution from robk on the php.net platform:

easy way to execute conditional html / javascript / css or 
other language code with php if else:

<?php if (condition): ?>

html code to run if condition is true

<?php else: ?>

html code to run if condition is false

<?php endif ?>

A nice example because you often have to combine php with html.

And what about elseif?


Yes, there also are elseifs. They somehow work like elses. But it combines with if. An ifelse will be called if the previous if doesn't hold true. It would look something like that:

$a = 5;
$b = 6;
$c = 7;
if ($a > $b) {
    echo $a + $b;
} elseif ($b < $c) {
    echo $a + $b + $c:
} else {
    echo 0;
}

// The output will be???? (Put your answer as a reply)

So you see. Theoreticaly you can chain as many elseif as you want.

A little bit of logic


In if and elseif it is possible to chain the conditions. For example:

$a = 5;
$b = 5;
$c = 7;
$d = 1337;

if ($a == $b && $c < $d) {
    echo "This should show now." //because this is true
}

So now we have && which simbolizes a logical AND. Means that both querys have to be true in order to be completely true.

and.jpg
So A and B both have to be true in case for C to be true as well.

And also an example for OR:

$a = 5;
$b = 5;
$c = 7;
$d = 1337;

if ($a > $b || $c < $d) {
    echo "This should show now." // And it also does. $a isn't bigger than $b, but $c is smaller than $d
}

So the two | symbolize the logical OR. Either one or both statements have to be true in order to be the if statement to be completely true.

or.jpg
So if either A or B is true (or even both are true) then C is true too.

Some more things nice to know


!= corresponds to is not equal.
You can also use a ! to invert the result of a condition.

For example:

$a = 5;
$b= 6;
$c = !($a == $b); // $a == $b should return false, but through the ! the result is true. It would also work vise versa

Homework


Of course you have all read this post carefully and followed every step. So you have to reply only one thing I was asking for in this post. ;)


I was looking forward to write something about arrays and loops as well but that would make this post even longer than it is already. I will try to throw something out in the next days or next tuesday at the latest.

If you are new with my tutorials and don't know how to execute PHP code, then I encourage you to read this.
And of course feel free to follow @smooms for more and I appreciate every upvote and resteem to help other people learn to code PHP.

Since then and have a nice week.

45

comments