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.