Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

to you inside your script. Others communicate with either a system log or a remote
debugger. Practical approaches to debugging are addressed in Chapter 21, "Design."


assert(expression)


The assert function tests an expression. If the assertion is true, no action is taken and the
script continues. If the assertion is false, behavior is dictated by the assertion options. By
default, assertions are not active, which means they are simply ignored. Use
assert_options to activate them.


Assertions are a nice way to add error checking to your code, especially paranoid checks
that are useful during development but unneeded during production.


<?
//create custom assertion function
function failedAssertion($file, $line, $expression)
{
print("On line $line, in file '$file' ");
print("the following assertion failed:
'$expression'
\n");
}


//turn on asserts
assert_options(ASSERT_ACTIVE, TRUE);


//bail on assertion failure
assert_options(ASSERT_CALLBACK, "failedAssertion");


//assert a false expression
assert(1 == 2);
?>


value assert_options(integer flag, value)


Use assert_options to get and set assert flags. Table 8.8 lists the flags and their
meanings. The previous value is returned. Most of the options expect a boolean because
they are either on or off. The exception is the option for setting the callback function.
This option expects the name of a function to be called when an assertion fails. This
function will be called with three arguments: the filename, the line number, and the
expression that evaluated as FALSE.


value call_user_function(string function, ...)


Use call_user_function to execute a function you've defined. The function argument
names the function. Arguments to be passed to the function follow.


Table 8.8. Assert Options
Free download pdf