<HTML ><HEAD ><TITLE >assert</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="PHP Options&Information" HREF="ref.info.html"><LINK REL="PREVIOUS" TITLE="assert_options" HREF="function.assert-options.html"><LINK REL="NEXT" TITLE="dl" HREF="function.dl.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.assert-options.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.dl.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.assert" ></A >assert</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN73434" ></A ><P > (PHP 4 )</P >assert -- Checks if assertion is <TT CLASS="constant" ><B >FALSE</B ></TT ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN73438" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >assert</B > ( string|bool assertion)<BR ></BR ><P > <B CLASS="function" >assert()</B > will check the given <TT CLASS="parameter" ><I >assertion</I ></TT > and take appropriate action if its result is <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > If the <TT CLASS="parameter" ><I >assertion</I ></TT > is given as a string it will be evaluated as PHP code by <B CLASS="function" >assert()</B >. The advantages of a string <TT CLASS="parameter" ><I >assertion</I ></TT > are less overhead when assertion checking is off and messages containing the <TT CLASS="parameter" ><I >assertion</I ></TT > expression when an assertion fails. This means that if you pass a boolean condition as <TT CLASS="parameter" ><I >assertion</I ></TT > this condition will not show up as parameter to the assertion function which you may have defined with the <A HREF="function.assert-options.html" ><B CLASS="function" >assert_options()</B ></A > function, the condition is converted to a string before calling that handler function, and the boolean <TT CLASS="constant" ><B >FALSE</B ></TT > is converted as the empty string. </P ><P > Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be <TT CLASS="constant" ><B >TRUE</B ></TT > 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. </P ><P > 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. </P ><P > The behavior of <B CLASS="function" >assert()</B > may be configured by <A HREF="function.assert-options.html" ><B CLASS="function" >assert_options()</B ></A > or by .ini-settings described in that functions manual page. </P ><P > The <A HREF="function.assert-options.html" ><B CLASS="function" >assert_options()</B ></A > function and/or ASSERT_CALLBACK configuration directive allow a callback function to be set to handle failed assertions. </P ><P > <B CLASS="function" >assert()</B > 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! </P ><P > 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) </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN73470" ></A ><P ><B >Example 1. Handle a failed assertion with a custom handler</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Active assert and make it quiet assert_options (ASSERT_ACTIVE, 1); assert_options (ASSERT_WARNING, 0); assert_options (ASSERT_QUIET_EVAL, 1); // Create a handler function function my_assert_handler ($file, $line, $code) { echo "<hr>Assertion Failed: File '$file'<br> Line '$line'<br> Code '$code'<br><hr>"; } // Set up the callback assert_options (ASSERT_CALLBACK, 'my_assert_handler'); // Make an assertion that should fail assert ('mysql_query ("")'); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.assert-options.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.dl.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >assert_options</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.info.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >dl</TD ></TR ></TABLE ></DIV ></BODY ></HTML >