<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >assert</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" 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=UTF-8"></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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.assert-options.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.dl.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.assert" ></A >assert</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN180567" ></A ><P > (PHP 4, PHP 5)</P >assert -- Checks if assertion is <TT CLASS="constant" ><B >FALSE</B ></TT ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN180571" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >assert</B > ( mixed assertion )<BR ></BR ><P > <B CLASS="function" >assert()</B > will check the given <CODE CLASS="parameter" >assertion</CODE > and take appropriate action if its result is <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > If the <CODE CLASS="parameter" >assertion</CODE > is given as a string it will be evaluated as PHP code by <B CLASS="function" >assert()</B >. The advantages of a string <CODE CLASS="parameter" >assertion</CODE > are less overhead when assertion checking is off and messages containing the <CODE CLASS="parameter" >assertion</CODE > expression when an assertion fails. This means that if you pass a boolean condition as <CODE CLASS="parameter" >assertion</CODE > 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 ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN180602" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >assertion</CODE ></DT ><DD ><P > The assertion. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN180611" ></A ><H2 >返回值</H2 ><P > <TT CLASS="constant" ><B >FALSE</B ></TT > if the assertion is false, <TT CLASS="constant" ><B >TRUE</B ></TT > otherwise. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN180616" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN180619" ></A ><P ><B >例 1. Handle a failed assertion with a custom handler</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Active assert and make it quiet<br /></font><font color="#0000BB">assert_options</font><font color="#007700">(</font><font color="#0000BB">ASSERT_ACTIVE</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">assert_options</font><font color="#007700">(</font><font color="#0000BB">ASSERT_WARNING</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br /></font><font color="#0000BB">assert_options</font><font color="#007700">(</font><font color="#0000BB">ASSERT_QUIET_EVAL</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Create a handler function<br /></font><font color="#007700">function </font><font color="#0000BB">my_assert_handler</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#0000BB">$line</font><font color="#007700">, </font><font color="#0000BB">$code</font><font color="#007700">)<br />{<br /> echo </font><font color="#DD0000">"<hr>Assertion Failed:<br /> File '$file'<br /><br /> Line '$line'<br /><br /> Code '$code'<br /><hr />"</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// Set up the callback<br /></font><font color="#0000BB">assert_options</font><font color="#007700">(</font><font color="#0000BB">ASSERT_CALLBACK</font><font color="#007700">, </font><font color="#DD0000">'my_assert_handler'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Make an assertion that should fail<br /></font><font color="#0000BB">assert</font><font color="#007700">(</font><font color="#DD0000">'mysql_query("")'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></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" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.dl.html" ACCESSKEY="N" >下一页</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" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >dl</TD ></TR ></TABLE ></DIV ></BODY ></HTML >