Advertisement
PHP if else and if elseif else statement
Okay now back to basics.
Many of new php programmers dont even know the uses of PHP’s If else and elseif Statement.
So, I thought it would be good to teach them..
We are going to use the built in PHP function rand() to get results of flipping a coin.
Then we will use PHP if else statement to check if the result is equal to 1.
These are pretty self explainary, if something is true, then execute some, else (if it isnt true) then execute something else.
PHP Code:
< ?php
$coin_flip = rand(1,2); // Flip a coin, and take random values "1 or 2".
if ($coin_flip == 1) // If value is 1
{
echo "You flipped a coin and it turns out to be heads";
} else { // if value is not 1
echo ("You flipped a coin and it turns out to be a tails");
}
?>
Ok, this is pretty much self explainary, we first used PHP’s builtin function rand(), to get random values between 1 and 2 , by using rand(1,2) , then we used if else statement to match our results.
The if else statement’s working is very simple, it shows “You flipped a coin and it turns out to be heads” if the random value of $coin_flip is equal to 1 , else it shows “You flipped a coin and it turns out to be a tails” which probably that $coin_flip is not equal to 1.
Now we will do something with if elseif else statement.
It is used to get results in some conditions, let me give you an example:
PHP Code:
< ?php
$days = array('monday', 'tuesday', 'friday');
$day = array_rand($days, 1);
$today = $days[$day];
if ($today == 'monday') // If $day is monday
{
echo 'Hello! Today is Monday';
}
elseif ($today == 'friday') // if $day is friday
{
echo 'Hello! Today is Friday';
} else { // if its not monday or tuesday, get this
echo 'Hello! Today isnt Monday or Friday, but Tuesday';
}
?>
Now this code was simple it explains itself very clearly.
We first defined $days as array (mixture of strings) , then we used another PHP’s builtin function array_rand() to take a single value from $days and assigned that value to $day and then we assigned the $day’s value to $today, in that case, we used it like this : array_rand($days, 1);
Now we used if elseif else statement to define our conditions.
If random value of $today is equal to ‘monday’ echo blah blah blah, and if the random value of $today is equal to ‘friday’ then echo blah blah blah and if none of above conditions apply, echo blah blah blah
Pretty simple, isnt it ?
You noticed we used curly braces( { } ) in these statements,They are “consequences” (as such) from the if/else statement. However, we can use it without the braces too, to make our code shorter, let me give you an example:
PHP Code:
< ?php
$a = 6;
$b = 5;
$is_a_bigger = ($a > $b) ? “yep” : “nope”;
echo $is_a_bigger;
?>
This code is simple, it has 3 parts.
The first part is condition, then you can see there is a questin mark ( ? ) it means “then” and then there is a colon ( : ) which means “else”.
Its very easy to understand and you can notice it shinked our code in one line
So keep practicing and remember “Practice makes Perfect”.