Home >Backend Development >PHP Tutorial >php--declare statement

php--declare statement

伊谢尔伦
伊谢尔伦Original
2016-11-24 09:14:571609browse

declare structure is used to set the execution instructions of a piece of code. The syntax of declare is similar to other flow control structures: the

declare (directive)
    statement

directive part allows setting the behavior of the declare section. Currently, only two instructions are known: ticks and encoding.

Note: encoding is a new command in PHP 5.3.0.

The statement part of the declare code segment will be executed - how it is executed and what side effects occur during execution depend on the instructions set in the directive.

The declare structure can also be used in the global scope, affecting all subsequent code (but if a file with a declare structure is included by other files, it will not affect the parent file containing it).

<?php
// these are the same:
// you can use this:
declare(ticks=1) {
    // entire script here
}
// or you can use this:
declare(ticks=1);
// entire script here
?>

Ticks

Tick (clock cycle) is an event that occurs every time the interpreter executes N timeable low-level statements in the declare code segment. The value of N is specified with ticks=N in the directive part of declare.

Not all statements can be timed. Usually conditional expressions and parameter expressions are not timeable.

The events that occur in each tick are specified by register_tick_function(). See the example below for more details. Note that multiple events can occur in each tick.

Example #1 Tick usage example

<?php
declare(ticks=1);
// A function called on each tick event
function tick_handler()
{
    echo "tick_handler() called\n";
}
register_tick_function(&#39;tick_handler&#39;);
$a = 1;
if ($a > 0) {
    $a += 2;
    print($a);
}
?>

Example #2 Ticks usage example

<?php
function tick_handler()
{
  echo "tick_handler() called\n";
}
$a = 1;
tick_handler();
if ($a > 0) {
    $a += 2;
    tick_handler();
    print($a);
    tick_handler();
}
tick_handler();
?>

See register_tick_function() and unregister_tick_function().

Encoding

You can use the encoding directive to specify the encoding method for each script.

Example #3 Specify the encoding method for the script

ea7038b934caf55ed7f035f208f239fa

Caution declare when combined with a namespace The only legal syntax is declare(encoding='...');, where ... is the encoded value. And declare(encoding='...') {} will produce a parsing error when combined with a namespace.


In PHP 5.3, the encoding value in declare will be ignored unless --enable-zend-multibyte is specified when compiling.

Note that unless phpinfo() is used, PHP will not show whether --enable-zend-multibyte was specified during compilation.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn