assert
Checks if assertion is &false;
&reftitle.description;
boolassert
mixedassertion
stringdescription
assert will check the given
assertion and take appropriate action if
its result is &false;.
If the assertion is given as a string it
will be evaluated as PHP code by assert.
The advantages of a string assertion are
less overhead when assertion checking is off and messages
containing the assertion expression when
an assertion fails. This means that if you pass a boolean condition
as assertion this condition will not show up as
parameter to the assertion function which you may have defined with the
assert_options function, the condition is converted
to a string before calling that handler function, and the boolean &false;
is converted as the empty string.
Assertions should be used as a debugging feature only. You may
use them for sanity-checks that test for conditions that should
always be &true; and that indicate some programming errors if not
or to check for the presence of certain features like extension
functions or certain system limits and features.
Assertions should not be used for normal runtime operations like
input parameter checks. As a rule of thumb your code should
always be able to work correctly if assertion checking is not
activated.
The behavior of assert may be configured by
assert_options or by .ini-settings described
in that functions manual page.
The assert_options function and/or
ASSERT_CALLBACK configuration directive allow a
callback function to be set to handle failed assertions.
assert callbacks are particularly useful for
building automated test suites because they allow you to easily
capture the code passed to the assertion, along with information
on where the assertion was made. While this information can be
captured via other methods, using assertions makes it much faster
and easier!
The callback function should accept three arguments. The first
argument will contain the file the assertion failed in. The
second argument will contain the line the assertion failed on and
the third argument will contain the expression that failed (if
any — literal values such as 1 or "two" will not be passed via
this argument). Users of PHP 5.4.8 and later may also provide a fourth
optional argument, which will contain the
description given to assert, if
it was set.
&reftitle.parameters;
assertion
The assertion.
description
An optional description that will be included in the failure message if
the assertion fails.
&reftitle.returnvalues;
&false; if the assertion is false, &true; otherwise.
&reftitle.changelog;
&Version;
&Description;
5.4.8
The description parameter was added. The
description is also now provided to a callback
function in ASSERT_CALLBACK mode as the fourth
argument.
&reftitle.examples;
Handle a failed assertion with a custom handler
Assertion Failed:
File '$file'
Line '$line'
Code '$code'
";
}
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// Make an assertion that should fail
assert('mysql_query("")');
?>
]]>
Using a custom handler to print a description
]]>
&example.outputs;
&reftitle.seealso;
assert_options