note 88929 added to control-structures.declare

From: Date: Fri, 13 Feb 2009 17:06:02 +0000
Subject: note 88929 added to control-structures.declare
Groups: php.notes 
Request: Send a blank email to php-notes+get-150629@lists.php.net to get a copy of this message
Note that the two methods for calling declare are not identical. Method 1: <?php // Print "tick" with a timestamp and optional suffix. function do_tick($str = '') { list($sec, $usec) = explode(' ', microtime()); printf("[%.4f] Tick.%s\n", $sec + $usec, $str); } register_tick_function('do_tick'); // Tick once before declaring so we have a point of reference. do_tick('--start--'); // Method 1 declare(ticks=1); while(1) sleep(1); /* Output: [1234544435.7160] Tick.--start-- [1234544435.7161] Tick. [1234544435.7162] Tick. [1234544436.7163] Tick. [1234544437.7166] Tick. */ ?> Method 2: <?php // Print "tick" with a timestamp and optional suffix. function do_tick($str = '') { list($sec, $usec) = explode(' ', microtime()); printf("[%.4f] Tick.%s\n", $sec + $usec, $str); } register_tick_function('do_tick'); // Tick once before declaring so we have a point of reference. do_tick('--start--'); // Method 2 declare(ticks=1) { while(1) sleep(1); } /* Output: [1234544471.6486] Tick.--start-- [1234544472.6489] Tick. [1234544473.6490] Tick. [1234544474.6492] Tick. [1234544475.6493] Tick. */ ?> Notice that when using {} after declare, do_tick wasn't auto-called until about 1 second after we entered the declare {} block. However when not using the {}, do_tick was auto-called not once but twice immediately after calling declare();. I'm assuming this is due to how PHP handles ticking internally. That is, declare() without the {} seems to trigger more low-level instructions which in turn fires tick a few times (if ticks=1) in the act of declaring. ---- Server IP: 208.69.120.35 Probable Submitter: 168.122.64.150 ---- Manual Page -- http://www.php.net/manual/en/control-structures.declare.php Edit -- https://master.php.net/note/edit/88929 Del: integrated -- https://master.php.net/note/delete/88929/integrated Del: useless -- https://master.php.net/note/delete/88929/useless Del: bad code -- https://master.php.net/note/delete/88929/bad+code Del: spam -- https://master.php.net/note/delete/88929/spam Del: non-english -- https://master.php.net/note/delete/88929/non-english Del: in docs -- https://master.php.net/note/delete/88929/in+docs Del: other reasons-- https://master.php.net/note/delete/88929 Reject -- https://master.php.net/note/reject/88929 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150629) next »