
|
|
|||
Introduction to PHP ProgrammingBy PHP Catalyst2007-11-19
Control Structures in PHP Flow-Control Statements (Control Structures)As you get deeper into programming, you will see a need of executing a series of statements based on conditions. Based on your requirement, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces like { and }. if construct
if (expr)
statement If the expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. Let us look at a simple logical flow before we attempt to write PHP code for the same. In fact, when I started to learn programming, I first started to write simple english statements and then wrote coding for them. So, here it is: Step1 - I want to check if value of $a is bigger than 5 Now, writing the same code, programmatically:
<?phpIn the above code, the expression is evaluated to either TRUE or FALSE and in this case, if value of $a is found to be bigger than 5, it evaluates to TRUE and then the statements within the statement-group are then executed. In this example, we have already assigned a value of 6 to $a, but you should try to assign values less than 5 and test the results. As we said earlier, you can group more statememts in the statement-group and execute them. Try the example and check the result:
<?phpYou could also do the following:
<?phpNow, if you were to execute the above code, it will print the string "Bigger than 5" and then assign the value of $a to $b. Have some fun with this code, try this:
<?phpHere, if $a is bigger than 5, value of $a is assigned to $b and the same is printed on screen. Change the value of $a in first line to 4 and see what happens? I hope, you had a good learning session with if construct. If you need more examples in this section, please contact us and let us know. You now know how to execute statement(s) if certain condition is met. In this section we will execute a statement in case the expression in the if statement evaluates to FALSE. Let's start with looking with the example that we had used earlier:
<?phpIn the above example, swap the values of $a and $b and then check the results. I hope, you are understanding the use of else construct. Often, we want to execute statement, when the expression in if statement is evaluated to TRUE. But, in case, we want to execute a different set of statement when the expression is evaluated to FALSE and that's when else plays a role. Remember that else statements are executed ONLY when the expression in if statement is evaluated to FALSE. There is another construct called 'elseif' which is a combination of if and else. Unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. See this example elseif statement for better understanding:
<?phpIn this example, the if construct evaluates if $a is bigger than $b. If TRUE, it will execute the statement within the curly brackets which is "a is bigger than b". If found to be FALSE, it will move onto elseif construct which will then evaluate the condition to check if values of $a and $b are equal (note the use of ==, Comparison Operators) and if found TRUE will execute the statement "a is equal to b". If FALSE, it will move onto the else construct which does not evaluate any condition but will execute the statement "a is smaller than b" because the condition of if previous expression was evaluated to be false. Try changing the values of $a and $b to understand this better. while statementUsing while statement is very simple. Let's first look at the basic syntax/form of while statement:
while (expr)
statement In while statement, group of statement(s) are executed as long as the expression in while statement evaluates to TRUE. The condition and its evaluation is checked at the beginning of each iteration. A simpler example of while statement would be: Step1 - Assign value of 1 to $i Writing it using while statement would look something like:
<?phpdo-while loopLet us start with the syntax of do-while loops:
do {
statement } while (expr); The only difference between a while and do-while statement is that
expression is evaluated at the end of each iteration instead of doing
it at the beginning. In a do-while loop, the statement will run the
first time and when it reaches end of statements the expression is
evaluated. Based on the exression being TRUE or FASLE, this iteration
is either repeated or ends.
<?phpIn the example, it first prints the value of $i, then increments it by 1 and then evaluates the expression in while statement. Try changing the value of $i from 1 to 10 and see what happens? for loopsfor loops are more complex in strcuture than other loop strutures in PHP. The general syntax for a for loop is:
for (expr1; expr2; expr3)
statement Here, the first expression (expr1) is executed without any conditions at the very beginning of the loop. Then at the beginning of each iteration, the second expression (expr2) is evaluated and if found to be TRUE, the nested statements are executed. This is one iteration cycle. If the expression (expr2) is evaluated to be false, the execution of the loop ends there. And, at the end of each iteration, the third expression (expr3) is executed. In this example, the first expression is '$i=1' which is executed without any conditions. Now the iteration begins, and at the beginning of the iteration, the second expression $i less than or equal to 10 is evaluated. Since it evaluates to FALSE ($i is 1 and is smaller than 10), the statement 'echo $i' is executed and then the last expression '$i++' is executed thus incrementing the value of $i by 1. Now, we are inside the loop, and second expression is evaluated, is found false, prints the value of $i and increments by 1. This iteration continues till the second expression evaluates to be TRUE which is value $i being equal to 10. foreach constructPHP provides us with an easy way to iterate over arrays. Since we have not convered arrays so far, we will skip this construct here for now. You may however visit the PHP documentation site to read about foreach construct. switch statementOne of the most easiest statement is switch statement. It matches the value of the variable in context and if the value matches, it then executes certain code depending on what you want it to execute. All switch statements evaluate every case with a comparision operator '=='. This might cause some probelm with comparing string to integers. Let's understand switch statement with an example:
<?phpSwitch statements are not good only for strings but also holds equally good for numbers. See this example:
<?phpswitch statements can also be empty, like this:
<?phpWhen a case is not found, you may find it easier to handle with 'default':
<?phpTutorial Pages: » What can I do with PHP? » Popoular Features of PHP » Basics of PHP » Variables in PHP » Data Types in PHP » Expressions and Operators » Control Structures in PHP » Functions in PHP » Declaring Functions in PHP » Scope of a Variable » Built-in Functions in PHP » Handling Strings in PHP » Printing Strings in PHP » String Comparisons in PHP » Manipulating Strings in PHP » Arrays in PHP » Types of Arrays in PHP » Creating Arrays in PHP » Array Operations in PHP |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |