}
?>
So, this time you check whether $i is greater than or equal to 0 and subtract 1 from it with each loop iteration. while
loops are typically used when you are unsure of how many times the code needs to loop because while
keeps looping until an external factor stops it.
With a for
loop, you specify precise limits on its operation by giving it a declaration, a condition, and an action. That is, you specify one or more variables that should be set when the loop first runs (the declaration), you set the circumstances that will cause the loop to terminate (the condition), and you tell PHP what it should change with each loop iteration (the action). That last part is what really sets a for loop apart from a while
loop: You usually tell PHP to change the condition variable with each iteration.
You can rewrite the script that counts down from 10 to 0 using a for
loop:
<?php
for($i = 10; $i >= 0; $i -= 1) {
echo $i;
}
?>
This time you do not need to specify the initial value for $i
outside the loop, and neither do you need to change $i
inside the loop — it is all part of the for
statement. The actual amount of code is really the same, but for this purpose the for loop is arguably tidier and therefore easier to read. With the while
loop, the $i
variable was declared outside the loop and so was not explicitly attached to the loop.
The third loop type is foreach
, which is specifically for arrays and objects, although it is rarely used for anything other than arrays. A foreach
loop iterates through each element in an array (or each variable in an object), optionally providing both the key name and the value.
In its simplest form, a foreach
loop looks like this:
<?php
foreach($myarr as $value) {
echo $value;
}
?>
This loops through the $myarr
array created earlier, placing each value in the $value
variable. You can modify that to get the keys as well as the values from the array, like this:
<?php
foreach($myarr as $key => $value) {
echo '$key is set to $value
';
}
?>
As you can guess, this time the array keys go in $key and the array values go in $value
. One important characteristic of the foreach
loop is that it goes from the start of the array to the end and then stops — and by start we mean the first item to be added rather than the lowest index number. This script shows this behavior:
<?php
$array = array(6 => 'Hello', 4 => 'World',
2 => 'Wom', 0 => 'Bat');
foreach($array as $key => $value) {
echo '$key is set to $value
';
}
?>
If you try this script, you will see that foreach
prints the array in the original order of 6, 4, 2, 0 rather than the numerical order of 0, 2, 4, 6.
If you ever want to exit a loop before it has finished, you can use the same break statement you saw earlier to exit a switch/case
block. This becomes more interesting if you find yourself with nested loops — loops inside of loops. This is a common situation to be in. For example, you might want to loop through all the rows in a table and, for each row in that table, loop through each column. Calling break
exits only one loop or switch/case
, but you can use break 2
to exit two loops or switch/cases
, or break 3
to exit three, and so on.
Including Other Files
Unless you are restricting yourself to the simplest programming ventures, you will want to share code among your scripts at some point. The most basic need for this is to have a standard header and footer for your website, with only the body content changing. However, you might also find yourself with a small set of custom functions you use frequently, and it would be an incredibly bad move to simply copy and paste the functions into each of the scripts that use them.
The most common way to include other files is with the include
keyword. Save this script as include1.php
:
<?php
for($i = 10; $i >= 0; $i -= 1) {
include 'echo_i.php';
}
?>
Then save this script as echo_i.php
:
<?php
echo $i;
?>
If you run include1.php
, PHP loops from 10 to 0 and includes echo_i.php
each time. For its part, echo_i.php
just prints the value of $i
, which is a crazy way of performing an otherwise simple operation, but it does demonstrate how included files share data. Note that the include
keyword in include1.php
is inside a PHP block, but PHP is reopened inside echo_i.php
. This is important because PHP exits PHP mode for each new file, so you always have a consistent entry point.
Basic Functions
PHP has a vast number of built-in functions that enable you to manipulate strings, connect to databases, and more. There is not room here to cover even 10% of the functions; for more detailed coverage of functions, check the 'Reference' section at the end of this chapter.
Strings
Several important functions are used for working with strings, and there are many less frequently used