34,333
edits
Changes
no edit summary
One of the main reasons for using scripting languages such as PHP is to build logic and intelligence into the creation and deployments deployment of web based data. In order to be able to build logic into PHP based scripts, it is necessary for the script to be able to make decisions and repeast repeat tasks based on specified criteria.
For example, it may be necessary to repeat a section of a script a specified number of times, or perform a task only if one or more conditions are found to be true (i.e only let the user log in if a valid passowrd has been provided).
Just about everything in life revolves around decisions. We wouldn't get very far through the day if we weren't able to decide what to wear, what to eat and which roads to traverse on the way to work. Similarly computers would be of little use without some innate ability to evaluate choices and make decisions. If all we were able to do was provide a computer with a set of instructions that simply followed one after the other with no ability to make choices based on specific criteria, none of the software we have today would exist.
Conditional statements provide the core of building decision making into scripting languages such as PHP. Conditional statements essentially control whether a part of a script is executed depending the result of a particular expression (i.e whether an expression returns a boolean ''true'' or ''false'' value). The two types of conditional structures provided by PHP (and most other programming langaugeslanguages) are ''if'' and ''if ... else''. In this section we will take a close look at each of these condtional structures, and also provide some examples of their use.
=== The PHP ''if'' Statement ===
== PHP ''while'' loops ==
The PHP ''for'' loop described previously works well when you know in advance how many times a particular task needs to be repeated in a script. Clearly, there will frequently be instances where code needs to be repeated until a certain condition is met, with no way of knowing in advance how many repetitions are going to be needed to meet that criteria. To address this PHP, provides the ''while'' loop.
Essentially, the ''while'' loop repeats a set of tasks until a specified condition is met. The ''while'' loop syntax is defined follows:
</pre>
In the above example the ''while'' expression will evaluate whether $myCount is less than 100. If it is already greater than 100 the code in the braces is skipped and the loop exits without performing any tasks. If $myCount is not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of $myCount. This process repeats until $myCount is greater than 100, at which point the loop exits.
=== PHP ''do ... while'' loops ===
</pre>
There can be any number of ''case'' statements - basically as many as you need to fully compare the ''value'' in the ''switch'' statement against the possible options (represented by ''match1'' through to ''match5'' in the above example) specified by the ''case'' statements. When a match is found the ''PHP statements'' corresponding to the matching case are executed. Note the ''break;'' statement in each case - this is important. Without the ''break;'' all the cases after the matching case will also be executed when a amtch match is found.
The ''default'' statement specifies the action that should be taken if no match is found. The following provides an example of the ''switch'' statement in action:
</pre>
In the above example the break will break out of the inner loop when $x is equal to 10 but the outer ''for'' loop will continue to run. This is fine if that is the desired behavior, but is a problem if the outer loop needs to also be broken at this point. This problem can be resolved using the ''break'' statemnt statement followed by the number of loops you from which you wish to break out. The syntax for this typew type of break is:
break ''n'';
where ''n'' represents the number of loop levels from which to break. With this knowledge we can modify our previous example to break out of both loops using a ''break 2;'' statement:
<pre>
<?php