Page 1 of 1

Interesting results using the "declare" construct...

Posted: Mon Jan 18, 2010 8:48 pm
by infolock
All,
I'm currently writing a small article on using the "declare" construct, but I've run into something that has puzzled me. When issuing the following code, I notice the expected 2 results of "I was called from a tick!" statement.

Code: Select all

 
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}
 
register_tick_function("my_callback_function");
declare(ticks = 1) {
 
$x = 10;
}
?>
 
However, when I run this set of code, I get 3!

Code: Select all

 
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}
 
register_tick_function("my_callback_function");
declare(ticks = 1) {
// $x = 10;
}
?>
 
This also results with 3!

Code: Select all

 
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}
 
register_tick_function("my_callback_function");
declare(ticks = 1) {
}
?>
 
I realize this must be because of some low-level instructions that PHP is executing, but what are those low-level instructions that execute on a blank declare or is this just a bug?


-Jon

Re: Interesting results using the "declare" construct...

Posted: Mon Jan 18, 2010 8:56 pm
by Weirdan
empty declare is taken as file-scope declare, which is evident from this code:

Code: Select all

 
<?php
function my_callback_function() {
    echo "I was called from a tick!<br />";
}
 
register_tick_function("my_callback_function");
declare(ticks = 1) {
    //$x = 10;
}
$i = 10;
while ($i-->0) {
    $x = 10;
}
 
Try changing the value of $i and see number of tick function calls changing. It's probably a bug.

Re: Interesting results using the "declare" construct...

Posted: Mon Jan 18, 2010 9:03 pm
by infolock
Cool. Thanks man. This was driving me insane since I accidently just ran it as a blank declare block, just for the hell of it to see what would happen ;) haha...

Thanks again!