declare

declare 结构用来设定一段代码的执行指令。declare 的语法和其它流程控制结构相似:

declare (directive)
    statement

directive 部分允许设定 declare 代码段的行为。目前只认识一个指令:ticks(更多信息见下面 ticks 指令)。

declare 代码段中的 statement 部分将被执行――怎样执行以及执行中有什么副作用出现取决于 directive 中设定的指令。

declare 结构也可用于全局范围,影响到其后的所有代码。

<?php
// these are the same:

// you can use this:
declare(ticks=1) {
    
// entire script here
}

// or you can use this:
declare(ticks=1);
// entire script here
?>

Ticks

Tick 是一个在 declare 代码段中解释器每执行 N 条低级语句就会发生的事件。N 的值是在 declare 中的 directive 部分用 ticks=N 来指定的。

在每个 tick 中出现的事件是由 register_tick_function() 来指定的。更多细节见下面的例子。注意每个 tick 中可以出现多个事件。

例子 16-3. 评估一段 PHP 代码的执行时间

<?php
// A function that records the time when it is called
function profile($dump = FALSE)
{
    static
$profile;

    
// Return the times stored in profile, then erase it
    
if ($dump) {
        
$temp = $profile;
        unset(
$profile);
        return (
$temp);
    }

    
$profile[] = microtime();
}

// Set up a tick handler
register_tick_function("profile");

// Initialize the function before the declare block
profile();

// Run a block of code, throw a tick every 2nd statement
declare(ticks=2) {
    for (
$x = 1; $x < 50; ++$x) {
        echo
similar_text(md5($x), md5($x*$x)), "<br />;";
    }
}

// Display the data stored in the profiler
print_r(profile (TRUE));
?>
这个例子评估“declare”中的 PHP 代码,每执行两条低级语句就记录一次时间。此信息可以用来找到一段特定代码中速度慢的部分。这个过程也可以用其它方法完成,但用 tick 更方便也更容易实现。

Ticks 很适合用来做调试,以及实现简单的多任务,后台 I/O 和很多其它任务。

参见 register_tick_function()unregister_tick_function()


add a note add a note User Contributed Notes
aeolianmeson at NOSPAM dot blitzeclipse dot com
30-May-2006 03:06
The scope of the declare() call if used without a block is a little unpredictable, in my experience. It appears that if placed in a method or function, it may not apply to the calls that ensue, like the following:

function a()
{
   declare(ticks=2);
   b();
}

function b()
{
   // The declare may not apply here, sometimes.
}

So, if all of a sudden the signals are getting ignored, check this. At the risk of losing the ability to make a mathematical science out of placing a number of activities at varying durations of ticks like many people have chosen to do, I've found it simple to just put this at the top of the code, and just make it global.
warhog at warhog dot net
19-Dec-2005 04:39
as i read about ticks the first time i thought "wtf, useless crap" - but then i discovered some usefull application...

you can declare a tick-function which checks each n executions of your script whether the connection is still alive or not, very usefull for some kind of scripts to decrease serverload

<?php

function check_connection()
{ if (
connection_aborted())
   {
// do something here, e.g. close database connections
     // (or  use a shutdown function for this
    
exit; }
}

register_tick_function("connection");

declare (
ticks=20)
{
 
// put your PHP-Script here
  // you may increase/decrease the number of ticks
}

?>
fredrik at demomusic dot nu
10-Aug-2005 07:30
Beware! Using ticks on Windows, running Apache as a php-module, crashes Apache and will not work.

You have to use the CGI-version to use ticks on Windows.

More information here:
http://bugs.php.net/bug.php?id=26771
chris-at-free-source.com
01-Mar-2005 04:16
Also note that PHP is run in a single thread and so everything it does will be one line of code at a time.  I'm not aware of any true threading support in PHP, the closest you can get is to fork.

so, declare tick doens't "multi-thread" at all, it is simply is a way to automaticaly call a function every n-lines of code.
fok at nho dot com dot br
08-Jul-2003 09:45
This is a very simple example using ticks to execute a external script to show rx/tx data from the server

<?php

function traf(){
 
passthru( './traf.sh' );
  echo
"<br />\n";
 
flush(); // keeps it flowing to the browser...
 
sleep( 1 );
}

register_tick_function( "traf" );

declare(
ticks=1 ){
  while(
true ){}  // to keep it running...
}

?>

contents of traf.sh:
# Shows TX/RX for eth0 over 1sec
#!/bin/bash

TX1=`cat /proc/net/dev | grep "eth0" | cut -d: -f2 | awk '{print $9}'`
RX1=`cat /proc/net/dev | grep "eth0" | cut -d: -f2 | awk '{print $1}'`
sleep 1
TX2=`cat /proc/net/dev | grep "eth0" | cut -d: -f2 | awk '{print $9}'`
RX2=`cat /proc/net/dev | grep "eth0" | cut -d: -f2 | awk '{print $1}'`

echo -e "TX: $[ $TX2 - $TX1 ] bytes/s \t RX: $[ $RX2 - $RX1 ] bytes/s"
#--= the end. =--
daniel@swn
02-Feb-2003 03:56
<?php
ob_end_clean
();
ob_implicit_flush(1);

function
a() {
 for(
$i=0;$i<=100000;$i++) { }
 echo
"function a() ";
}
function
b() {
 for(
$i=0;$i<=100000;$i++) { }
 echo
"function b() ";
}

register_tick_function ("a");
register_tick_function ("b");

declare (
ticks=4)
{
   while(
true)
   {
      
sleep(1);
       echo
"\n<br><b>".time()."</b><br>\n";;
   }
}
?>
You will see that a() and b() are slowing down this process. They are in fact not executed every second as expected. So this function is not a real alternative for multithreading using some slow functions..there is no difference to this way: while (true) { a(); b(); sleep(1); }
xxoes
08-Jan-2003 06:23
If i use ticks i must declare all functions before i call the function.

example:

Dosn't work
<?php
function ticks() {
   echo
"tick";
}
register_tick_function("ticks");

declare (
ticks=1) 1;

echo
"";
echo
"";

foo(); // Call to undefined function.

function foo() {
   echo
"foo";
}
?>

Work
<?php
function ticks() {
   echo
"tick";
}
register_tick_function("ticks");

//declare (ticks=1) 1;

echo "";
echo
"";

foo();

function
foo() {
   echo
"foo";
}
?>

win2k : PHP 4.3.0 (cgi-fcgi)
rob_spamsux at rauchmedien dot ihatespam dot com
19-Mar-2002 06:45
Correction to above note:

Apparently, the end brace '}' at the end of the statement causes a tick.

So using

------------
declare (ticks=1) echo "1 tick after this prints";
------------

gives the expected behavior of causing 1 tick.

Note: the tick is issued after the statement executes.

Also, after playing around with this, I found that it is not really the multi-tasking I had expected. It behaves the same as simply calling the functions. I.e. each function must finish before passing the baton to the next function. They do not run in parallel.

It also seems that they always run in the order in which they were registered.

So,

<?php
------------
# register tick functions
register_tick_function ("a");
register_tick_function ("b");

# make the tick functions run
declare (ticks=1);
?>
------------

is equivalent to

------------
a();
b();
------------

It is simply a convenient way to have functions called periodically while some other code is being executed. I.e. you could use it to periodically check the status of something and then exit the script or do something else based on the status.
rob_spamsux at rauchmedien dot ihatespam dot com
19-Mar-2002 05:58
Here is an example of multi-tasking / multi-threading:

<?php
# declare functions
function a() {
  echo
"a";
}
function
b() {
  echo
"b";
}

# register tick functions
register_tick_function ("a");
register_tick_function ("b");

# make the tick functions run
declare (ticks=1);

# that's all there is to it.
?>

Notes:
This will make functions a and b run once each at the same time.

If you try:

declare (ticks=1) {
  1;
}

They will run twice each. That is because it seems to be an undocumented fact that there is always an extra tick.

Therefore:

declare (ticks=2) {
  1;
}

Will cause them to run once.