HOME       >>       Programming

Php: Lesson #2; If...else Statements. Basics of If...Else statements.


Alright, this is part #2 of my PHP Lessons. We will go into depth with more of the, scripting part of PHP.


Things you should know.

1. Basic PHP

2. Understanding of scripting.


If...Else statements

1. If...Else statements are used if you want your script/scripts to do something based on a variable.

<?phpif (condition)code to be done if condition is true;elsecode to be done if condition is false;?>
2. With that, you're probably a bit still confused as to what it's doing, I was too at first. Look at the next code for a better explanation. This is if there was a form prior to this page, that form, asking what is 4+5, their answer being $a.

<?php$a = 4+5;if($a == 9)echo "You are correct, 4+5=9!";elseecho "You are not correct, please go back and try again.";?>
3. If the persons answer is 9, the page will say 'You are correct, 4+5=9!', if not, it will say 'You are not correct, please go back and try again.'

4. Now, the If...elseif...else statement.

<?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Mon") echo "Have a nice week!"; else echo "Have a nice day!"; ?>
5. If the day is Friday, it says 'Have a nice weekend!', if the day is Monday, it will say 'Have a nice week!', and if the day is not Friday or Monday, it says 'Have a nice day!'.


That's it for If...Else statements, next lesson would be on Arrays, but I don't like Arrays so it'll be made later on, next Lesson will be on functions.


if ($d=="Fri")

I guess that this supposes that you defined somewhere that your current language is English ? Else, if the language comes from your operating systems default, it could be french, and then the day is "Ven" and not "Fri" ?


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.