<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >declare</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Handleiding" 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="require" HREF="function.require.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 Handleiding</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="control-structures.switch.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Hoofdstuk 11. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.require.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.declare" ><VAR CLASS="literal" >declare</VAR ></A ></H1 ><P > The <VAR CLASS="literal" >declare</VAR > construct is used to is used to set execution directives for a block of code. The syntax of <VAR CLASS="literal" >declare</VAR > is similiar to the syntax of other flow control constructs: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4333" ></A ><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 <VAR CLASS="literal" >directive</VAR > section allows the behavior of the <VAR CLASS="literal" >declare</VAR > block to be set. Currently only one directive is recognized: the <VAR CLASS="literal" >ticks</VAR > directive. (See below for more information on the <A HREF="control-structures.declare.html#control-structures.declare.ticks" >ticks</A > directive) </P ><P > The <VAR CLASS="literal" >statement</VAR > part of the <VAR CLASS="literal" >declare</VAR > block will be executed - how it is executed and what side-effects occur during execution may depend on the directive set in the <VAR CLASS="literal" >directive</VAR > block. </P ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="control-structures.declare.ticks" >Ticks</A ></H2 ><P >A tick is an event that occurs for every <VAR CLASS="replaceable" >N</VAR > low-level statements executed by the parser within the <VAR CLASS="literal" >declare</VAR > block. The value for <VAR CLASS="replaceable" >N</VAR > is specified using <VAR CLASS="literal" >ticks=<VAR CLASS="replaceable" >N</VAR ></VAR > within the <VAR CLASS="literal" >declare</VAR > blocks's <VAR CLASS="literal" >directive</VAR > section. </P ><P > The event(s) that occurs on each tick is 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="AEN4357" ></A ><P ><B >Voorbeeld 11-1. Profile a section of PHP code</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <pre><br /><font color="#0000BB"><?php<br /></font><font color="#FF8000">// A function that records the time when it is called<br /></font><font color="#007700">function </font><font color="#0000BB">profile </font><font color="#007700">(</font><font color="#0000BB">$dump </font><font color="#007700">= </font><font color="#0000BB">FALSE</font><font color="#007700">)<br />{<br /> static </font><font color="#0000BB">$profile</font><font color="#007700">;<br /><br /> </font><font color="#FF8000">// Return the times stored in profile, then erase it<br /> </font><font color="#007700">if (</font><font color="#0000BB">$dump</font><font color="#007700">) {<br /> </font><font color="#0000BB">$temp </font><font color="#007700">= </font><font color="#0000BB">$profile</font><font color="#007700">;<br /> unset (</font><font color="#0000BB">$profile</font><font color="#007700">);<br /> return (</font><font color="#0000BB">$temp</font><font color="#007700">);<br /> }<br /><br /> </font><font color="#0000BB">$profile</font><font color="#007700">[] = </font><font color="#0000BB">microtime </font><font color="#007700">();<br />}<br /><br /></font><font color="#FF8000">// Set up a tick handler<br /></font><font color="#0000BB">register_tick_function</font><font color="#007700">(</font><font color="#DD0000">"profile"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Initialize the function before the declare block<br /></font><font color="#0000BB">profile </font><font color="#007700">();<br /><br /></font><font color="#FF8000">// Run a block of code, throw a tick every 2nd statement<br /></font><font color="#007700">declare (</font><font color="#0000BB">ticks</font><font color="#007700">=</font><font color="#0000BB">2</font><font color="#007700">) {<br /> for (</font><font color="#0000BB">$x </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">; </font><font color="#0000BB">$x </font><font color="#007700">< </font><font color="#0000BB">50</font><font color="#007700">; ++</font><font color="#0000BB">$x</font><font color="#007700">) {<br /> echo </font><font color="#0000BB">similar_text </font><font color="#007700">(</font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">$x</font><font color="#007700">), </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">$x</font><font color="#007700">*</font><font color="#0000BB">$x</font><font color="#007700">)), </font><font color="#DD0000">"<br>"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">// Display the data stored in the profiler<br /></font><font color="#0000BB">print_r </font><font color="#007700">(</font><font color="#0000BB">profile </font><font color="#007700">(</font><font color="#0000BB">TRUE</font><font color="#007700">));<br /></font><font color="#0000BB">?><br /></font></pre></font> </code></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" >Terug</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Begin</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.require.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><VAR CLASS="literal" >switch</VAR ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" ACCESSKEY="U" >Omhoog</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.require.html" ><B CLASS="function" >require()</B ></A ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >