What is the Declare keyword used in PHP?

13

After all, what is the declare keyword used in PHP?

I've seen explanations out there, including in the PHP Manual , but not yet I found it very clear.

Can it be useful for development?

Example:

declare (ticks=1) {
    // todo script aqui
}
    
asked by anonymous 26.02.2015 / 15:38

2 answers

15

Well, in the PHP manual we have

  

The declare statement is used to define execution directives for a block of code.

Below we have the following sentence

  

The directive section allows the behavior of the declare block to be defined. Currently two 'directive' are recognized: the 'directive' ticks (...) and the 'directive' encoding (...)

Which in the case does not explain anything

Everything below is an explanation of how each of the directive works.

By the explanation and testing it is very confusing to know why we use directive ticks however from what I understand you use declare(ticks=N) to define how many functions you need to use to print what you registered. Example:

function tick_handler(){
   echo "tick_handler() called <br>";
}
//crieu uma função para registrar em um tick

register_tick_function('tick_handler');
//registrei a função no tick

$a = 10;
//atribui um valor para a variavel

declare(ticks=1){
   if ($a > 0) {
       $a += 2;
       print($a.'<br>');
   }
}

The above example will bring the following result:

tick_handler() called  //desaparece caso apague a linha 14
12                     //desaparece caso apague a linha 15
tick_handler() called  //desaparece caso apague a linha 15
tick_handler() called  //desaparece junto dos anterios caso apague o if
tick_handler() called  //sempre são exibidos
tick_handler() called 

In what way does directive ticks intrude into the above code? Simple, the value of ticks defines how many ciclos nativos(chamadas do sistema não contam) must be executed in order for register_tick_function to be executed.

That's why each line of deleted code reduces the number of responses. and increasing the tick value increases the number of cycles required for it to execute the registered function call.

Note that if you bring line 9 into the scope of the declare you will have another run of register_tick_function

OBS2: If you do an infinite and empty while it will execute register_tick_function every cycle.

What's the use of this? If you are using a native function and have no idea how it is executed by PHP you can use ticks to know how many cycles it took to execute.

Worth it? I believe that no, we can use microtime to know how long it takes for the server to process anything without an overhead, since running a function every%% of cycles will be increasing the time to have an answer, leaving it with a failure , as you can see, the 2 cycles being displayed even though no function is called within the scope of N , we do not have an accuracy of how many cycles it is really taking to execute our function

  

NOTE   Each statement that PHP executes (except for some exceptions) is a "native cycle"   Thanks @bfavaretto

     

NOTE²   Ticks to be phased out in PHP 6.
  Source: TuxRadar

     

NOTA³   PHP 6 will no longer be released and it is not possible to tell at the moment if the Ticks will be present in PHP 7


What about declare ?
There is not much to talk about. The only thing it does is set the declare(encoding='...') of the page. You can not use it to set a encoding different for a certain part of the code (I would be very happy if I could) because when you try to do this the following error occurs:
encoding

  

OTHER NOTE According to a comment in the php manual it overrides zend.script_encoding set in php.ini

I found this question in the gringo OS about declare encoding with no response yet.

    
26.02.2015 / 17:52
2

In PHP 7 another directive was added: strict_types . This yes I see to be quite useful in development because it "undoes" a part of the "mess" of types that PHP has, leaving the developer a little more in control of the application than before.

Considering the function:

function increment(int $x): int
{
    return $x + 1;
}

echo increment('1'); // 2

See working at Repl.it | Ideone

Of course, PHP tries to cast cast from the input value to the type it expects; that is, if it called increment('1') , PHP would treat the string as an integer and would return 2 as if nothing had happened.

When using declare(strict_types=1) , you tell the interpreter that you want to work in a restricted type, where PHP will stop doing magic . The same example above, with strict_types would be:

declare(strict_types=1);

function increment(int $x): int
{
    return $x + 1;
}

echo increment('1');

See working at Repl.it | Ideone

In this case, a fatal error would be given:

  

PHP Fatal error: Uncaught TypeError: Argument 1 passed to increment () must be of type integer, string given, called in ...

The same works for the return type of the function which, if set, must be followed or otherwise will return a fatal error.

03.12.2018 / 20:03