control structure - Understanding PHP declare() and ticks -
today looking through php manual , stumbled upon control structure declare.
the declare construct used set execution directives block of code
this declare
supposed do. honest didn't understood it. on reading again found new thing ticks
a tick event occurs every n low-level tickable statements executed parser within declare block. value n specified using ticks=n within declare block's directive section.
i didn't understand either. mean n low-level tickable statements
if there had been sample code, have been easy understand. none found in manual. have found on q1, increased curiosity , confusion. can , can use this.
my actual confusion statement (from linked post) you can declare tick-function checks each n executions of script whether connection still alive or not
. when register tick function tick = 20 on php file , execute it, file alive till 20 execution complete(got idea when wrongly considered multithreaded). idea have got, dont think correct..
or simple replacement while($x = 20)
[edit 1]
have seen implementation of declare()
section of php manual function arguments
[edit 2]
in process control
you use declare() statement specify locations in program callbacks allowed occur. allows minimize overhead of handling asynchronous events
when php executing script, execution can seen lot of statements being executed. statements cause tick, though not statements so. (manual says: typically, condition expressions , argument expressions not tickable.
)
this block cause 5 ticks, executing 5 statements:
$a = 1; $b = 2; $a = 3; $b = 4; $a = 5;
and block cause 5 ticks, , 1 more tick end of while loop counted statement/tick:
while ($i < 5) $a++;
with of declare(ticks=n)
, register_tick_function()
, can execute code in between statements/ticks. register_tick_function specifies function should called when tick event occurs. , declare sets how many tick should pass, before tick event occurs.
with declare(ticks=1)
, register_tick_function('somefunction');
call somefunction()
code in between every statement/tick.
if use declare(ticks=3)
, somefunction()
executed on every third statement/tick.
example:
function handler(){ echo "x"; } register_tick_function("handler"); $i = 0; declare(ticks = 4) { while ($i < 9) echo ++$i; }
this script output: 1234x5678x9
it's simple.
now meant in linked question "whether connection still alive", not interesting on , not related above mentioned. on every tick event. can totally different. mentioned scripts can take quite time execute , during execution, client can disconnect. (imagine closing browser, while script still running.) php default continue run script, if client has disconnected. can use function connection_aborted()
detect if client has disconnected. without using ticks @ all.
now let's example want script stop running client disconnects. use ...
function killme() { if (connection_aborted()) { die(); } } register_tick_function('killme'); declare(ticks=1);
... , script call killme()
after each statement of code. killme()
check if client still connected , die()
when isn't.
Comments
Post a Comment