Specifications
Case and Function Names
Note that calls to functions are not case sensitive, so calling function_name(),
Function_Name(), or FUNCTION_NAME() are all valid and will all have the same result. You are
free to capitalize in any way you find easy to read, but you should aim to be consistent. The
convention used in this book, and most other PHP documentation, is to use all lowercase.
It is important to note that function names behave differently to variable names. Variable
names are case sensitive, so $Name and $name are two separate variables, but Name() and
name() are the same function.
In the preceding chapters, you have seen many examples using some of PHP’s built-in func-
tions. However, the real power of a programming language comes from being able to create
your own functions.
Why Should You Define Your Own Functions?
The functions built in to PHP enable you to interact with files, use a database, create graphics,
and connect to other servers. However, in your career there will be many times when you will
need to do something that the language’s creators did not foresee.
Fortunately, you are not limited to using the built-in functions because you can write your own
to perform any task that you like. Your code will probably be a mixture of existing functions
combined with your own logic to perform a task for you. If you are writing a block of code for
a task that you are likely to want to reuse in a number of places in a script or in a number of
scripts, you would be wise to declare that block as a function.
Declaring a function allows you to use your own code in the same way as the built-in func-
tions. You simply call your function and provide it with the necessary parameters. This means
that you can call and reuse the same function many times throughout your script.
Basic Function Structure
A function declaration creates or declares a new function. The declaration begins with the key-
word function, provides the function name, the parameters required, and contains the code
that will be executed each time this function is called.
Here is the declaration of a trivial function:
function my_function()
{
echo “My function was called”;
}
Using PHP
P
ART I
132
07 7842 CH05 3/6/01 3:35 PM Page 132