Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 227

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!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&nbsp;--&nbsp;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
>&#13;   <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
>&#13;   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
>&#13;   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
>&#13;   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
>&#13;   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
>&#13;   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
>&#13;   <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
>&#13;   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
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>assertion</CODE
></DT
><DD
><P
>&#13;       The assertion.
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN180611"
></A
><H2
>返回值</H2
><P
>&#13;   <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
>&#13;   <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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;hr&gt;Assertion Failed:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File '$file'&lt;br /&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line '$line'&lt;br /&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Code '$code'&lt;br /&gt;&lt;hr /&gt;"</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">?&gt;</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
>