Web Design for BeginnersFree top tips for web designers |
Creating an IF ELSE statementBy James Middleton - Added 11th of January 2009Sometimes it we may want to filter out certain actions within our web-page based upon previously established variables. A very simple method to asking the great 'If it is this, do this, else, do that', we can use the following: $number = 10; if($number == 10) { echo "The number is Ten"; } else { echo "This number is not Ten!"; } # The presence of the double '==' means that the value after it must completely match the string before it - if $string is totally equal (or the sames as) to 10. The else offers an option to do something else if the first argument fails. Now try the following. We can use greater than (>) or lesser than (<) within our if else statement. $number = 5; if($number >= 5) { echo "The number is five."; } else { echo "This number is not five!"; } The argument above reads: if number is greater or equal to 5...We could also omit the '=' if we wanted the argument to pick up on only larger numbers. We can also extend the if else statement for more complex arguments with the use of 'elseif': $name = "John"; if($name == "Sarah") { echo "Hello Sarah."; } elseif($name == "Peter") { echo "Hello Peter."; } elseif($name == "John") { echo "Hello John."; } else { echo "Hello, whoever you are!";} How about adding to the argument: $name = "Sarah"; if($name == "Sarah" || $name == "Peter")) { echo "You are either Sarah or Peter."; } The '||' represents 'or' and you can add as many of these connectors as you like within your if statement. To implement an 'and' try the following: $price = "240"; $size = "25"; if($price < 250 && $size > 20)) { echo "I think I have found what you are looking for"; } else { echo "Nope, nothing here"; } Notice the use of "" within our statements. Generally, you use quotation marks on words, sentences and sequences of numbers. You don't need to add quotation marks on single numbers. Credits & LinksArticle written by James Middleton - www.webdesign-4-beginners.co.uk.Information for PublishersThis article is copyrighted and you do not have consent to copy or redistribute it without written consent from the author.Add your own comments |
|
Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.
|