Programming in PHPBy Peter McNulty : 20-11-2004
4.28 (118)
Conditions
Wouldn't it be nice to make your program do one thing if a condition was met, and another thing if it wasn't? Well you can! This is the basis of all program flow in virtually all programming languages. We use conditions to make choices within programs to decide which direction to go. It is called the flow of control.
If statement <?php There it is – the flow of control! In this example, the words Bob is 23! will be printed to the screen, as $age = 23. You can see that the structure of the condition in PHP is: if (condition) { statement; statement; } The keyword if is used to tell PHP we are going to check a condition now. Then the condition itself is place inside brackets after this, and then the statements are surround by curly braces. One very important thing to notice here is the == symbol. This symbol means does a equal to b?. If we only use one equals sign, then the phrase would read a equals b and obviously, this cannot be a condition as it would not evaluate to true or false. Therefore, the golden rule for a condition, is that it MUST evaluate to true or false, so it must be like a yes / no question in English. Operators This of course brings up the question, can I check more than if they are just equal to each other? And naturally the answer to every rhetorical question in this article is yes! We have what are called operators. These operators include ==, <, <=, >, >=, != and ===. So far we have already encountered the == operator. The greater than (>) and greater than or equal to (>=) operators compare the sizes of the values you have passed. The less than operators perform the exact opposite: Operators <?php In this example, both of the conditions would evaluate to true, so both of the strings of text would be printed to the screen. As for the other two operators, != means, does a NOT equal b and === means, are a and b identically equal. This second one may be difficult to understand, but as PHP is dynamically typed then an integer could equal a float: <?php In the above example script, we have two variables of which one is an integer and one is a float. This is only determined by the value as the float has a decimal place of zero. The first condition above is going to validate as 23.0 is technically equal to 23. However, the second condition will not. It checks both the value and the type of the variables that are being checked against. Multiple Conditions A common task you might want to do, is to check two or more conditions are met before executed some statements. This can be accomplished using the AND and OR keywords. In some scripts these will be replaced with && and || respectively and mean the exact same thing. Multiple Conditions <?php See in the above how we have put two conditions into the condition block. We surround each condition with brackets (often for readability) and place the AND or OR keywords between them. In the above, both will evaluate to true, so both strings will be printed to screen. Choice of two paths Often we want to do one thing if a condition evaluates to true, and another if it evaluates to false. We could write two separate if statements however it is unnecessary. We simply use the else statement: If / Else <?php In this example, it shows that by placing the else statement after the closing curly bracket, and then surrounding your new statements by more curly brackets, we can get PHP to execute something upon failure. In this example, only the top would be executed, however, if we were to change $age = 20 then only the bottom would be executed. This of course leads to multiple conditions. It may be necessary to execute something different for every different possible value of your condition values. <?php That was an example of having the possibility of many different values for your conditions. You can add as many elseif statements as you like, and they perform exactly the same as if statements. The final else statement in this example is used to execute some code incase none of the previous conditions have been met. This is completely optional but you may have a use for it. Always remember to enclose your statements with curly braces so that PHP knows what part of code to execute for your conditions. I also added the switch control statements. This performs the exact same as the multiple if statements, however if usually provides a more elegant method of doing it. It is quite simple to understand, as you use the switch (variable name) format with the curly braces again. This time, we specified each condition as a 'case'. After we define the case, followed by a colon, we write the statements to be executed. We then use the break statement to get out of the switch case, which means we have finished executing. If we do not use the break keyword, then the next case will automatically be executed. The default case, is simply the equivalent as the final else in the if / elseif version of the code. Build Your Own Database Driven Website Using PHP & MySQL |
Ads
|