3rd tutorial on php: contents: 1. if statement 2. if-else statement 3. if-elseif statement
'if' statement is a decision making statement. Its syntax is: if(condition){ //block to be executed } In php how we use if statement lets have a look at it by a program. <?php $name = 'bob'; if($name=='bob'){ echo 'Hello bob'; } ?>
output: Hello bob //because variable name == bob let see another example where the condition is false: <?php $name = 'bob'; if($name=='Mike'){ echo 'Hello bob'; } ?> in this case nothing will be displayed because the condition is fails.
Now let move to words our next topic i.e. if-else statement It is same as if statement except it has an else block so that when the condition is false the control will be transfer to the else part. Let have an example: <?php $name = 'Mike'; if($name=='Mike'){ echo 'Hello Mike'; } else { echo 'Dear user you are not Mike'; } ?> output: Hello Mike because condition is true. Now, let see another example where condition is not true. <?php $name = 'bob'; if($name=='Mike'){ echo 'Hello Mike'; } else { echo 'Dear user you are not Mike'; //this line is executed this time } ?> output: Dear user you are not Mike
Now lets move towards the next content i.e. if-elseif. In if-elseif multiple conditions and block are available. lets see an example: <?php $name = 'bob'; if($name=='Mike'){ echo 'Hello Mike'; } elseif($name=='bob') { echo 'Hello Bob'; } else { echo 'You are neither Bob nor Mike'; } ?> output: Hello Bob elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.
Well thats it for this tutorial. I hope this tutorial will be interesting and helpful for you guys. Good luck. :)
for those who join the tutorial now can also see the previous tutorials. 1. http://openstudy.com/study#/updates/4ffa87a8e4b058f8b7666f25 2. http://openstudy.com/study#/updates/4ffb14c9e4b058f8b766d273
fantastic
thank you
@agdgdgdgwngo what do you think about the tutorials ???
Join our real-time social learning platform and learn together with your friends!