PHP: Conditionals - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: PHP: Conditionals (/showthread.php?tid=26570) |
PHP: Conditionals - Retribute - 06-21-2012 If you need help: feel free to PM me. Credits to: PHP, MySQL & JavaScript by O'Reilly and Tizag tutorials.
Introduction: Conditionals alter program flow and enable you to ask questions about certain things and respond to the answers you get in various ways. Conditionals are central to dynamic web pages - the goal of using PHP in the first place - because they make it easy to create different output each time a page is viewed. == The if Statement: One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It's pretty much a straight line, but now and then you'll encounter various signs telling you where to go. Now imagine coming across a detour sign that you have to follow if a certain condition is TRUE, you ignore the detour and carry on driving. The contents of the if condition can be any valid PHP expression, including equality, comparison, tests for zero and NULL, and even the values returned by functions. The action to take when an if conditions is TRUE are generally placed inside curly braces, { }. You can however ignore the braces if you have only a single statement to execute. It is a good practice, though, so I recommend using them. Example: PHP Code: <?php Alright, let's analyze my example. In this example, your conditional is if the day variable equals to Friday, it would echo out "It's Friday, Friday, gotta get down' on Friday!" If the condition is not met, nothing will be echo'd in this example. So basically, if your conditional is met, it'll do whatever is in the curly braces. Note:
The else Statement: Sometimes when a conditional is not TRUE, you may not want to continue on to the main program code immediately but might wish to do something else instead. With the else statement, you can set up a second detour on your highway. What happens with an if...else statement is that the first conditional statement is executed if the condition is TRUE, but if it's FALSE, the second one is executed. Only one of the two choices can be executed, under no circumstances can both (or neither) be executed. Example: PHP Code: <?php In this example, if your condition isn't met, it will execute your else statement, which echos out "G'day sir!". The elseif Statement: There are also times when you want a number of different possibilities to occur, based upon a sequence of conditions. You can achieve this using the elseif statement. It is like an else statement, except that you place a further conditional expression prior to the condition code. Example: PHP Code: <?php Switch Statements: With the use of the switch statement you can check for all these else...if conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation! The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time. Example: In our example the single variable will be $day and the cases will be: Friday, Saturday, and Monday. PHP Code: <?php Note:
RE: PHP: Conditionals - Kewlz - 06-21-2012 Wow these are similar to C++/VB. Thanks for this. RE: PHP: Conditionals - 911Computer - 06-27-2012 Kewlz. The sintaxis of php, c++, c#, java and pascal (maybe more) is similar. vb is too different to these, but in the use of variables not |