Interesting results using the "declare" construct...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Interesting results using the "declare" construct...

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

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

Post 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!
Post Reply