php--declare statement

伊谢尔伦
Release: 2016-11-24 09:14:57
Original
1579 people have browsed it

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
Copy after login

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
?>
Copy after login

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);
}
?>
Copy after login

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();
?>
Copy after login

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

declare(encoding='ISO-8859-1');
// code here
?>

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.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template