<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>declare</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.switch.html">switch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.return.html">return</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.declare" class="sect1"> <h2 class="title"><em>declare</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> The <em>declare</em> construct is used to set execution directives for a block of code. The syntax of <em>declare</em> is similar to the syntax of other flow control constructs: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> declare (directive) statement </pre></div> </div> </div> </p> <p class="para"> The <em>directive</em> section allows the behavior of the <em>declare</em> block to be set. Currently only two directives are recognized: the <em>ticks</em> directive (See below for more information on the <a href="control-structures.declare.html#control-structures.declare.ticks" class="link">ticks</a> directive) and the <em>encoding</em> directive (See below for more information on the <a href="control-structures.declare.html#control-structures.declare.encoding" class="link">encoding</a> directive). </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> The encoding directive was added in PHP 5.3.0 </span> </p></blockquote> <p class="para"> The <em>statement</em> part of the <em>declare</em> block will be executed - how it is executed and what side effects occur during execution may depend on the directive set in the <em>directive</em> block. </p> <p class="para"> The <em>declare</em> construct can also be used in the global scope, affecting all code following it (however if the file with <em>declare</em> was included then it does not affect the parent file). <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// these are the same:<br /><br />// you can use this:<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// entire script here<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// or you can use this:<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// entire script here<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <div class="sect2" id="control-structures.declare.ticks"> <h3 class="title">Ticks</h3> <p class="para">A tick is an event that occurs for every <var class="varname"><var class="varname">N</var></var> low-level tickable statements executed by the parser within the <em>declare</em> block. The value for <var class="varname"><var class="varname">N</var></var> is specified using <code class="code">ticks=<var class="varname"><var class="varname">N</var></var></code> within the <em>declare</em> blocks's <em>directive</em> section. </p> <p class="para"> Not all statements are tickable. Typically, condition expressions and argument expressions are not tickable. </p> <p class="para"> The event(s) that occur on each tick are specified using the <span class="function"><a href="function.register-tick-function.html" class="function">register_tick_function()</a></span>. See the example below for more details. Note that more than one event can occur for each tick. </p> <p class="para"> <div class="example" id="example-137"> <p><strong>Example #1 Tick usage example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// A function called on each tick event<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">()<br />{<br /> echo </span><span style="color: #DD0000">"tick_handler() called\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">register_tick_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'tick_handler'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$a </span><span style="color: #007700">+= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /> print(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-138"> <p><strong>Example #2 Ticks usage example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">()<br />{<br /> echo </span><span style="color: #DD0000">"tick_handler() called\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br /><br />if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$a </span><span style="color: #007700">+= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br /> print(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">tick_handler</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="simpara"> See also <span class="function"><a href="function.register-tick-function.html" class="function">register_tick_function()</a></span> and <span class="function"><a href="function.unregister-tick-function.html" class="function">unregister_tick_function()</a></span>. </p> </div> <div class="sect2" id="control-structures.declare.encoding"> <h3 class="title">Encoding</h3> <p class="para"> A script's encoding can be specified per-script using the encoding directive. <div class="example" id="example-139"> <p><strong>Example #3 Declaring an encoding for the script.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">encoding</span><span style="color: #007700">=</span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// code here<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <div class="caution"><strong class="caution">Caution</strong> <p class="simpara"> When combined with namespaces, the only legal syntax for declare is <em>declare(encoding='...');</em> where <em>...</em> is the encoding value. <em>declare(encoding='...') {}</em> will result in a parse error when combined with namespaces. </p> </div> <p class="para"> The encoding declare value is ignored in PHP 5.3 unless php is compiled with <em>--enable-zend-multibyte</em>. </p> <p class="para"> Note that PHP does not expose whether <em>--enable-zend-multibyte</em> was used to compile PHP other than by <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span>. </p> <p class="para"> See also <a href="ini.core.html#ini.zend.script-encoding" class="link">zend.script_encoding</a>. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.switch.html">switch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.return.html">return</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>