. 34
( 132 .)


mixed call_user_func_array (callback function [, array parameters])

This function is fundamentally the same as call_user_func(), with the differ-
ence that, instead of passing a single parameter or list of parameters, it passes an
array to the called function.

void die ([string status])

This function outputs the argument string and then terminates the script. It is
used very commonly to abort a function when an error is encountered.
Note that this function is equivalent to exit().


mixed eval (string code)

This function is used to execute code stored in a variable. The function evaluates
and executes the code string contained in the argument, and returns the return
value of the code.

Using eval() with poorly escaped or terminated code will result in parse
errors that can be tricky to debug.

func_get_arg(), func_get_args(), and func_num_args()
mixed func_get_arg (int arg_num)

array func_get_args (void)

int func_num_args (void)
Chapter 6: PHP™s Built-in Functions 177

When called within a function, these three functions give information about the
function™s arguments:

— func_get_args() returns an array consisting of the function™s argument
— func_get_arg() returns argument number arg_num in the function™s
argument list.
— func_num_args() returns the number of arguments passed to the function.

Each of these functions generates a warning if it is not called from within a
user-defined function.


bool function_exists (string functionname)

If the function specified in the argument exists, this function returns TRUE. This
function can be useful for testing the runtime environment, although some func-
tions that exist may not be usable in the current configuration.

include(), include_once(), require(), and require_once()
These are special language constructs used to control the execution of scripts. Each
takes a PHP file name as an argument, and includes and evaluates the file in the
course of execution. require() and include() behave identically, with the differ-
ence that require() will generate a fatal error if the file to be included is missing,
whereas include() merely generates a warning.
include_once() and require_once(), as their names imply, will only include
the specified file if it has not already been included.
The format is simple:


Random number generator functions
Every now and then you will need to pick something at random. It may be an indi-
vidual element, or it may be something that has to do with randomizing an array
with shuffle() or getting a random element from an array with array_rand(). In
any case you will need to make use of PHP™s random number generator functions.
Note that the random number generator needs to be seeded before use. That is, it
has to be given a number that is reasonably unique to begin with. For this, as you
will see, the microtime() function will be of great use.
178 Part II: Working with PHP

Keep in mind that really two sets of random number generators exist. There are
the standard rand(), which doesn™t take a seed, and srand(), which does and
which you need in order to seed the generator for shuffle() and array_rand().
However, if you just want to get a random number and not use it with any other
functions, use the mt functions described below ” they™re faster and more random.
Now we examine some important random number generator functions used in
the applications in this book.

This function seeds your random number generator.

void mt_srand (int seed)

Use the following line and you can be sure your numbers will be plenty random:

mt_srand ((double) microtime() * 1000000);

Seed the random number generator only once per script.

This function returns a random number. You can specify a minimum value and/or
a maximum value.

int mt_rand ([int min [, int max]])

So to get a random number between 1 and 100, do the following:

mt_srand((double)microtime() * 1000000);
$number = mt_rand(1,100);
echo $number;

int rand ([int min, int max])

rand() generates a (pseudo)random number between min and max.

mt_rand() returns better, faster results than rand().
Chapter 6: PHP™s Built-in Functions 179

Session functions
These are explained in detail in Chapter 14. Sessions are means by which state is
maintained between pages. Remember that HTTP, the language of the Web, does not
allow servers to remember much of anything between requests for pages from a
specific user. Sessions allow the server to keep track of activities by a single user.

MySQL functions
These functions are explained in detail in Appendix J.
A total of 48 MySQL functions are available in the library. Only about a third of
these are used in the applications in this book. You may find uses for some of the
other MySQL functions in your applications, but you probably won™t use all of them.

HTTP header functions
Three vital HTTP header functions exist, both of which you will need to get to know.

If you are going to be communicating with the browser or with other HTTP servers,
this is the function to use.

int header (string string)

Essentially, you can send any header that would be expected under RFC 2616
(ftp://ftp.isi.edu/in-notes/rfc2616.txt), which defines HTTP. The RFC itself
is a handful (and perhaps the sleepiest reading you™ll do all year). Here is a common
header you are likely to send:

header(“Location: http://www.php.net”);

This is nothing more than a redirect: It sends the browser to a page you specify.
If you have been working with straight HTML and JavaScript or the <META
type=refresh> tag to do your redirects, you should switch to this type of header
whenever possible. It will work for all browsers and the redirection will be totally
transparent to the user.

IMPORTANT ” no, make that VERY IMPORTANT ” you cannot send a header
after anything ” ANYTHING ” has been sent to the browser. If you send a
header after even a hard return, you will get an error. If you send a hard
return before your opening <?php tag, you will get an error. If there is a hard
return in an included file that precedes your header() function, you will
180 Part II: Working with PHP

get an error. This should not be a problem you encounter frequently; your
pages should be designed so that most of the logic is handled prior to the
display. However, if you have a situation you just can™t work around, take a
look at the output buffering functions.

This is basically a specialized header function, because a cookie is set by nothing
more than a specific HTTP header.

int setcookie (string name [, string value [, int expire [, string
path [, string domain [, int secure]]]]])

The first argument will be the name of the cookie. The second will be the value.
The expire value should be set with the time function. The following is a pretty
typical use of setcookie():


This will set a cookie that will expire in 24 hours (24 — 60 — 60). The cookie will
be available to every directory within domain.com. If you want to restrict it to a
specific directory, you can change the / to a directory name.

You can find more on cookies in Chapter 4 in the discussion on variables.

In some versions of Internet Explorer, you must either give both time and
path values or neither.

This function can keep you from sending headers after some text has been sent to
the browser.

bool header_sent(void)
Chapter 6: PHP™s Built-in Functions 181

If you are relying heavily on this function, you are probably not coding your
pages properly.

Image functions
PHP provides very powerful functions for generating and manipulating images.

Here is a rundown of the image functions used in this book.


array getimagesize (string filename [, array imageinfo])

Given an image file as argument, this function returns an array containing the
width of the image in pixels, the height of the image in pixels, a numeric flag con-
taining the type of the image, and a text string containing the dimensions that can
be used directly in an HTML IMG tag. Valid numeric flags include 1 for GIF, 2 for
JPEG, and 6 for BMP.
So, given a JPEG of 468 — 60 pixels, getimagesize() would return the following:

[0] => 468
[1] => 60
[2] => 2
[3] => height=”60” width=”468”


int imagecopyresized (resource dest_im, resource src_im, int destX,
int destY, int srcX, int srcY, int destW, int destH, int srcW, int

This function is used to copy part of one image (referred to above as a
resource), delineated by rectangular X and Y coordinates and offset, to another
image. The arguments it takes are as follows:

— The destination image identifier

— The source image identifier
182 Part II: Working with PHP

— The starting X coordinate on the destination image

— The starting Y coordinate on the destination image

— The starting X coordinate on the source image

— The starting Y coordinate on the source image

— The width in pixels of the destination image

— The height in pixels of the destination image


. 34
( 132 .)