HTML and CSS Reference
In-Depth Information
In this example, I'm searching for a particular color. When the foreach loop gets to the
array element that matches the color I'm looking for, I print the color out and use the
break statement to stop the loop. When I've found the element I'm looking for, there's
no reason to continue.
I could accomplish the same thing a different way using continue , like this:
$colors = ('red', 'green', 'blue');
$looking_for = 'red';
foreach ($colors as $color) {
if ($color != $looking_for) {
echo “Found $color.\n”;
In this case, if the color is not the one I'm looking for, the continue statement stops exe-
cuting the body of the loop and goes back to the loop condition. If the color is the one
I'm looking for, the continue statement is not executed and the echo function goes
ahead and prints the color name I'm looking for.
The loops I'm using as examples don't have a whole lot of work to do. Adding in the
break and continue statements doesn't make my programs much more efficient.
Suppose, however, that each iteration of my loop searches a very large file or fetches
some data from a remote server. If I can save some of that work using break and con-
tinue , it could make my script much faster.
Built-in Functions
PHP supports literally hundreds of built-in functions. You've already seen a few, such as
echo() and count() . There are many, many more. PHP has functions for formatting
strings, searching strings, connecting to many types of databases, reading and writing
files, dealing with dates and times, and just about everything in between.
You learned that most of the functionality in the JavaScript language is built using the
methods of a few standard objects such as window and document . PHP is different—
rather than its built-in functions being organized via association with objects, they are all
just part of the language's vocabulary.
If you ever get the feeling that there might be a built-in function to take care of some
task, check the PHP manual to see whether such a function already exists. Chances are it
does. Definitely check whether your function will manipulate strings or arrays. PHP has
a huge library of array- and string-manipulation functions that take care of most common
Search WWH ::

Custom Search