<HTML ><HEAD ><TITLE >declare</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="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="switch" HREF="control-structures.switch.html"><LINK REL="NEXT" TITLE="return" HREF="function.return.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" 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="control-structures.switch.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.return.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.declare" ></A ><TT CLASS="literal" >declare</TT ></H1 ><P > The <TT CLASS="literal" >declare</TT > construct is used to set execution directives for a block of code. The syntax of <TT CLASS="literal" >declare</TT > is similar to the syntax of other flow control constructs: <DIV CLASS="informalexample" ><A NAME="AEN5063" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >declare (directive) statement</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The <TT CLASS="literal" >directive</TT > section allows the behavior of the <TT CLASS="literal" >declare</TT > block to be set. Currently only one directive is recognized: the <TT CLASS="literal" >ticks</TT > directive. (See below for more information on the <A HREF="control-structures.declare.html#control-structures.declare.ticks" >ticks</A > directive) </P ><P > The <TT CLASS="literal" >statement</TT > part of the <TT CLASS="literal" >declare</TT > block will be executed -- how it is executed and what side effects occur during execution may depend on the directive set in the <TT CLASS="literal" >directive</TT > block. </P ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="control-structures.declare.ticks" ></A >Ticks</H2 ><P >A tick is an event that occurs for every <TT CLASS="replaceable" ><I >N</I ></TT > low-level statements executed by the parser within the <TT CLASS="literal" >declare</TT > block. The value for <TT CLASS="replaceable" ><I >N</I ></TT > is specified using <TT CLASS="literal" >ticks=<TT CLASS="replaceable" ><I >N</I ></TT ></TT > within the <TT CLASS="literal" >declare</TT > blocks's <TT CLASS="literal" >directive</TT > section. </P ><P > The event(s) that occur on each tick are specified using the <A HREF="function.register-tick-function.html" ><B CLASS="function" >register_tick_function()</B ></A >. See the example below for more details. Note that more than one event can occur for each tick. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5087" ></A ><P ><B >Example 12-1. Profile a section of PHP code</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // A function that records the time when it is called function profile ($dump = FALSE) { static $profile; // Return the times stored in profile, then erase it if ($dump) { $temp = $profile; unset ($profile); return ($temp); } $profile[] = microtime (); } // Set up a tick handler register_tick_function("profile"); // Initialize the function before the declare block profile (); // Run a block of code, throw a tick every 2nd statement declare (ticks=2) { for ($x = 1; $x < 50; ++$x) { echo similar_text (md5($x), md5($x*$x)), "<br />;"; } } // Display the data stored in the profiler print_r (profile (TRUE)); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > The example profiles the PHP code within the 'declare' block, recording the time at which every second low-level statement in the block was executed. This information can then be used to find the slow areas within particular segments of code. This process can be performed using other methods: using ticks is more convenient and easier to implement. </P ><P > Ticks are well suited for debugging, implementing simple multitasking, backgrounded I/O and many other tasks. </P ><P > See also <A HREF="function.register-tick-function.html" ><B CLASS="function" >register_tick_function()</B ></A > and <A HREF="function.unregister-tick-function.html" ><B CLASS="function" >unregister_tick_function()</B ></A >. </P ></DIV ></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="control-structures.switch.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.return.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >switch</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >return</TD ></TR ></TABLE ></DIV ></BODY ></HTML >