Detailed php declare the syntax

declare


(The PHP. 4, the PHP. 5, the PHP. 7)
DECLARE configured to set the execution instruction code blocks. declare the syntax is similar to the syntax of other flow control structures:

declare (directive)
   statement

The directive section allows the behavior of the declare block. Currently known only three instructions: ticks (for more information see the ticks directive), encoding (for more information see encoding instructions) and strict_types (for more information see strict instructions)


A, ticks instructions

grammar:

<?php
declare (ticks=N);
# ...

// 或下面写法作用于代码块

declare (ticks=N){
    # ...
}

Zend Engine function of a low-level statements executed once went register_tick_function () registered per execution. Each can be roughly understood as an execution php code (e.g.: $ num = 1;) go to the next tick function execution has registered. Use of a certain control code execution time is, for example, the following code while the last has a loop, but the execution time is not more than 5 seconds.

The instruction with register_tick_function()the use of

// 下面是一个示例
declare(ticks=1);

class Counter {
    private $counter = 0;

    public function increase($a)
    {
        $this->counter+=$a;
    }
    
    public function print()
    {
        return $this->counter;    
    }
}

$obj = new Counter;
register_tick_function([&$obj, 'increase'],1);

for ($i = 0; $i < 100; $i++)
{
    $a = 3;
}

// unregister_tick_function([&$obj, 'increase']);
// Not sure how to de-register, you can use static methods and members in the Counter instead.

var_dump("Number of basic low level operations: " . $obj->print());

Other examples of point CSDN> the PHP DECLARE (ticks = N); Effect

Two, encoding instructions

grammar:

declare(encoding='ISO-8859-1');
# ...

It can be used to encode each script instruction encoding specified script.

Caution

When the only legal syntax and namespaces are combined declare declare (encoding = '...') ;, wherein ... is encoded values. And declare (encoding = '...') {} parsing error generated in combination with the namespace.

Unless specified --enable-zend-multibyte in compile time, or else declare the encoding value will be ignored in PHP 5.3.
Note Unless using phpinfo (), PHP does not show otherwise specified whether --enable-zend-multibyte in compile time.
See zend.script_encoding.

Three, strict_types instruction

grammar:

declare(strict_types=1);
# ...

By default, all PHP files are in a weak type checking mode. New declareinstructions, by specifying strict_typesa value (0 or 1), a type indicative of a strict check mode, acting on the function call and return statement; 0 indicates a weak type check mode.
declare (strict_types=1)It must be the first statement of the file. If this statement appears elsewhere in the document, will produce a compilation error, block mode is expressly prohibited.
Similar encodinginstructions, but different ticksinstructions, strict_typescommands affect only the file designated use, which will not affect it contains (by way include the like) other documents coming. The instructions compiled at run time can not be modified. It works, is to set a flag in the opcode, so that the function call and return type checking compliance with the type of constraint.

Example 1:
declare(strict_types=1);
 
function func(int $num): int {
    return $num;
}
var_dump(func(2)); // returned value:int(2)
Example Two:
//declare(strict_types=1);
 
function func(int $num):int{
    return $num;
}
var_dump(func(2.5)); // returned value:int(2)
Example Three:
declare(strict_types=1);
 
function func($num):int{
    return $num;
}
var_dump(func(2.5)); // returned value:Return value of func() must be of the type integer, float returned
Published 41 original articles · won praise 21 · views 70000 +

Guess you like

Origin blog.csdn.net/u010324331/article/details/88316692