For those of you who know Perl, you will see that PHP has borrowed alot from Perl. Infact the first PHP interpreter was written using Perl, the syntax is very similar in alot of ways.
The foreach function is for use with arrays. It allows you to go through each item in an array.
Go through each value in $fruits and call it $fruit. The above code would output:
The first parameter in the foreach() function tells PHP which array to go through. The second parameter tells PHP which variable to assign the value to as it goes through them. So as foreach() handles the first array item, $fruit contains "apple". As foreach() handles the second array item, $fruit contains "pear". This repeats until each item in the array has been processed.
Foreach is a very useful control structure. In the above script, if, on its travels, foreach finds an array value that is equal to "apple" the if statement is true and it will print out that the array contains "apple". It will go through each array item and check if the conditional statement is true.
the while() function has the syntax:
// PHP code here
This function does exactly what it says. WHILE the condition is true it will execute the PHP code contained within the curly braces.
WHILE 1 is equal to 1, print out "1 is equal to 1, duh!". 1 will always be equal to 1, so this while() function would loop through over and over, forever. The while() function will execute until the condition within the brackets is false.
The while() function checks "is the value stored in $number less than 10?" it contains 0, so it is less than 10. The statements between the curly braces are then executed. We print out the value stored in $number and then increment it (add one to it). $number is now 1, the function checks is it still less that 10? Yes, it prints it out and increments it again, this repeats until the value stored in $number is no longer less than ten. When $number is equal to nine, it is printed out and then incremented to ten, it is then no longer less than 10, the loop exits and the script continues.
We could also get the same results as above using the for() function. Here is the format:
Firstly we intialise, then we enter under which condition to execute the for loop, then we define an action to be executed each time we go through the loop.
This would output:
$number = 0; - Firstly we intialise the variable $number with a value of 0.
$number < 10; - Then we tell the for function to keep executing the code in the curly braces until the conditional statement $number < 10 is no longer true.
$number++ - Lastly we define the action, in this case we tell the for function to increment $number each time it goes through a loop.
Thus we get the results above, set $number to zero. While $number is less than 10, execute the code in the curly braces, at the start of each loop add 1 to $number. This results in the for function printing out the value stored in $number then looping through and adding 1 to $number until $number is not less than 10, then the loop exits and the script continues.
No Comments for this page.