ptg16476052
666 LESSON 24: Taking Advantage of the Server
In this case, if the color is not the one I’m looking for, the continue statement stops
executing 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
continue, 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
tasks.
User-Defined Functions
PHP enables you to create user-defined functions that, like JavaScript functions, enable
you to package up code you want to reuse. Here’s how a function is declared:
function myFunction($arg = 0) {
// Do stuff
}
The function keyword indicates that you’re creating a user-defined function. The name
of the function follows. In this case, it’s myFunction. The rules for function names and
variable names are the same—numbers, letters, and underscores are valid. The list of
arguments that the function accepts follows the function name, in parentheses.