declare

(PHP 4, PHP 5, PHP 7)

declare ½á¹¹ÓÃÀ´É趨һ¶Î´úÂëµÄÖ´ÐÐÖ¸Áî¡£declare µÄÓï·¨ºÍÆäËüÁ÷³Ì¿ØÖƽṹÏàËÆ£º

declare (directive)
    statement

directive ²¿·ÖÔÊÐíÉ趨 declare ´úÂë¶ÎµÄÐÐΪ¡£Ä¿Ç°Ö»ÈÏʶÁ½¸öÖ¸Áticks£¨¸ü¶àÐÅÏ¢¼ûÏÂÃæ ticks Ö¸ÁÒÔ¼° encoding£¨¸ü¶àÐÅÏ¢¼ûÏÂÃæ encoding Ö¸Á¡£

Note: encoding ÊÇ PHP 5.3.0 ÐÂÔöÖ¸Áî¡£

declare ´úÂë¶ÎÖÐµÄ statement ²¿·Ö½«±»Ö´ÐЗ—ÔõÑùÖ´ÐÐÒÔ¼°Ö´ÐÐÖÐÓÐʲô¸±×÷ÓóöÏÖÈ¡¾öÓÚ directive ÖÐÉ趨µÄÖ¸Áî¡£

declare ½á¹¹Ò²¿ÉÓÃÓÚÈ«¾Ö·¶Î§£¬Ó°Ïìµ½ÆäºóµÄËùÓдúÂ루µ«Èç¹ûÓÐ 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 ÖпÉÒÔ³öÏÖ¶à¸öʼþ¡£

Example #1 Tick µÄÓ÷¨Ê¾Àý

<?php

declare(ticks=1);

// A function called on each tick event
function tick_handler()
{
    echo 
"tick_handler() called\n";
}

register_tick_function('tick_handler');

$a 1;

if (
$a 0) {
    
$a += 2;
    print(
$a);
}

?>

Example #2 Ticks µÄÓ÷¨Ê¾Àý

<?php

function tick_handler()
{
  echo 
"tick_handler() called\n";
}

$a 1;
tick_handler();

if (
$a 0) {
    
$a += 2;
    
tick_handler();
    print(
$a);
    
tick_handler();
}
tick_handler();

?>

²Î¼û register_tick_function() ºÍ unregister_tick_function()¡£

Encoding

¿ÉÒÔÓà encoding Ö¸ÁîÀ´¶Ôÿ¶Î½Å±¾Ö¸¶¨Æä±àÂ뷽ʽ¡£

Example #3 ¶Ô½Å±¾Ö¸¶¨±àÂ뷽ʽ

<?php
declare(encoding='ISO-8859-1');
// code here
?>

Caution

µ±ºÍÃüÃû¿Õ¼ä½áºÏÆðÀ´Ê± declare µÄΨһºÏ·¨Óï·¨ÊÇ declare(encoding='...');£¬ÆäÖÐ ... ÊDZàÂëµÄÖµ¡£¶ø declare(encoding='...') {} ½«ÔÚÓëÃüÃû¿Õ¼ä½áºÏʱ²úÉú½âÎö´íÎó¡£

ÔÚ PHP 5.3 Öгý·ÇÔÚ±àÒëʱָ¶¨ÁË --enable-zend-multibyte£¬·ñÔò declare ÖÐµÄ encoding Öµ»á±»ºöÂÔ¡£

×¢Òâ³ý·ÇÓà phpinfo()£¬·ñÔò PHP ²»»áÏÔʾ³öÊÇ·ñÔÚ±àÒëʱָ¶¨ÁË --enable-zend-multibyte¡£

²Î¼û zend.script_encoding¡£

User Contributed Notes

calimero dot NOSPAM at NOSPAM dot creatixnet dot com 08-Oct-2017 12:29
As the date of this comment, this page doesn't include a link or any ref to the strict typing feature (which is also built upon the declare construct), which can be found at the page below :

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict

Already reported it as a bug, but thought having the link here might be of some use to those like me expecting to find it there.
ja2016 at wir dot pl 27-Aug-2017 01:38
Don't use uft-8 encoding with BOM. Then fatal error occurs ALWAYS. Substitute it with utf-8 without BOM.

---

*BOM*
<?php
declare(strict_types=1);
//Fatal error: strict_types declaration must be the very first statement in the script
wapmorgan at gmail dot com 01-Aug-2017 05:49
If you fork'ed (with pcntl_fork) after you've used `declare`, you need to do it again in child thread.

declare(ticks=1);
$pid = pcntl_fork();
if ($pid === 0) {
    declare(ticks=1);
} else {
    // code ..
}
Ionut 10-Feb-2017 03:36
I haven't found this written in the doc: when using declare in the global context, it has to be the first statement in the script, before any other output.

This works:

<?php
declare(strict_types=1);
function
sum(int $a, int $b): int {
    return
5.78;
}
# ... but fails when calling the function
?>

This doesn't work:

<html>
<body>
<?php
declare(strict_types=1);
function
sum(int $a, int $b): int {
    return
5.78;
}
# PHP Fatal error:  strict_types declaration must be the very first statement in the script [...]
?>
SteAp 05-Apr-2016 06:19
This helper class traces executed statements. Calls it like so:

       CStatementTracer::getInstance()->enableTrace( 'START' );

Source Code

<?php

 
declare(ticks=1);

class
CStatementTracer {
   
    static private
       
$_instance;
       
    static
$lastMessage;
   
    static
$skipFunctionNames;
   
    protected function
__construct( ) {

       
self::$skipFunctionNames = array(
        );
           
    }
// CStatData

        /**
     * Liefert die Instanz zur¨¹ck
     *
     * @return unknown
     */
   
static public function getInstance() {
       
       if (
self::$_instance === null ) {
           
self::$_instance = new CStatementTracer();
        }
        return
self::$_instance;
       
    }
   
    public function
addToSkipFunctionNames( $aName ) {
       
       
self::$skipFunctionNames[] = $aName;
       
    }
   
    public function
enableTrace() {
       
       
$this->writeMessage( '*************** START  ********************' );
       
       
register_tick_function( array( $this, 'traceCallback' ) );

    }
   
    public function
disableTrace() {

       
$this->writeMessage( 'STOP - ' );
       
       
unregister_tick_function( array( $this, 'traceCallback' ) );
       
       
    }

    public function
traceCallback( ) {
       
       
$trace = debug_backtrace();
       
       
$latestCall = $trace[ 1 ];
       
       
$shortenedArgs = array();
        foreach(
$latestCall['args'] as $noUseKey => $someArg ) {
           
            if (
is_object( $someArg ) ) {
               
$someArg = get_class( $someArg ) . '{}';
            } elseif (
is_array( $someArg ) ) {
               
$someArg = print_r( $someArg, TRUE );
            } elseif (
is_string( $someArg )) {
               
$someArg = '"' . $someArg . '"';
            }
           
           
$someArg = str_replace( "\n", ' ', $someArg );
           
$someArg = str_replace( "\r", ' ', $someArg );
           
$someArg = str_replace( "    ", ' ', $someArg );
           
$someArg = str_replace( "   ", ' ', $someArg );
           
$someArg = str_replace( "  ", ' ', $someArg );
           
            if (
80 < strlen( $someArg ) ) {
               
$someArg = substr( $someArg, 0, 15 ) . '...' . substr( $someArg, -15 );
            }
           
           
$shortenedArgs[] = $someArg;
           
        }

        if ( isset(
$latestCall['file'] )) {

           
$file = explode( '\\', $latestCall[ 'file' ] );
           
$file = $file[ count( $file ) - 1 ];
            if (
'' == $file ) {
               
$file = '{}';
            }
           
$file .= '/' . $latestCall[ 'line' ];

        } else {

           
$file = '[NpFile]';

        }
       
        if (  (
True === in_array( $latestCall['function'], self::$skipFunctionNames ) )
           ) {
            return;
        }
       
       
$args = implode( ', ', $shortenedArgs );
       
$newInfo = $file . ': ' . $latestCall['function'] . '( ' . $args . ' )';
       
       
$newInfo = str_pad( '', 2 * count( $trace ) ) . $newInfo;
       
       
$this->writeMessage( $newInfo );
    }

   
    public function
writeMessage( $someData ) {
       
        if (
self::$lastMessage == $someData ) {
            return;
        }
       
       
self::$lastMessage = $someData;
       
       
$logfilePath = dirname( __FILE__ ) . '/../statementTracer.log';
       
       
$fp = fopen( $logfilePath, 'a+' );
       
        if (
is_array( $someData ) ) {
           
fwrite( $fpprint_r( $someData, TRUE )  . "\n" );
        } else {
           
fwrite( $fp$someData  . "\n" );
        }
       
       
fclose( $fp );
    }  
    
   
}
// CStatementTracer
danekilian at wp dot pl 03-May-2015 01:13
We can specify different encoding to different blocks:

declare(encoding=ENCODING_VALUE) {
    //for a block
}

found this here:
http://code2care.org/tutorials/php/PHP-Declare-ticks-and-tick-functions-Statements-Tutorial.php
Kubo2 07-Apr-2015 02:13
Note that in PHP 7 <?php declare(encoding='...'); ?> throws an E_WARNING if Zend Multibyte is turned off.
php at niekbosch dot nl 19-Feb-2014 01:17
Basically 'declare( encoding = .... );' overrides the zend.script_encoding configuration option (as set in php.ini). However, keep in mind that:

* the file encoding must be compatible (at least in the ASCII range of characters) to the zend.script_encoding setting. If you set 'zend.script_encoding' to UTF-8 and save the file in UTF-16, PHP will not be able to interpret the file, let alone the declare statement. As long as you use ASCII compatible encodings (i.e. ISO-8859-1(5), UTF-8 etc) for both the file encoding as the zend.script_encoding, you should be fine. (However, I have not experimented with adding non-ascii characters in comments above the declare statement).

* PHP string literals are converted from your source code encoding (either set with the declare statement or else according to zend.script_encoding) to the mbstring.internal_encoding as set in your php.ini (even if you change the setting using mb_internal_encoding). As an example:

php.ini:
mbstring.internal_encoding = UTF-8

test.php:
<?php
declare(encoding = 'ISO-8859-15');
mb_internal_encoding( 'ISO-8859-15' );
echo
'a?a?' . "\n";
?>

This will still output the string UTF-8 encoded; in a terminal/browser with encoding 'ISO-8859-15' the string will look (something) like this: a?€a??
php dot net at e-z dot name 17-Oct-2013 03:34
you can register multiple tick functions:

<?PHP
function a() { echo "a\n"; }
function
b() { echo "b\n"; }

register_tick_function('a');
register_tick_function('b');
register_tick_function('b');
register_tick_function('b');

?>

will output on every tick:
a
b
b
b
sawyerrken at gmail dot com 09-Aug-2013 03:31
In the following example:

<?php
function handler(){
    print
"hello <br />";
}

register_tick_function("handler");

declare(
ticks = 1){
   
$b = 2;
}
//closing curly bracket tickable
?>

"Hello" will be displayed twice because the closing curly bracket is also tickable.

One may wonder why the opening curly bracket is not tickable if the closing is tickable. This is because the instruction for PHP to start ticking is given by the opening curly bracket so the ticking starts immediately after it.
Anonymous 27-Sep-2010 11:10
It's amazing how many people didn't grasp the concept here. Note the wording in the documentation. It states that the tick handler is called every n native execution cycles. That means native instructions, not including system calls (i'm guessing). This can give you a very good idea if you need to optimize a particular part of your script, since you can measure quite effectively how many native instructions are in your actual code.

A good profiler would take that into account, and force you, the developer, to include calls to the profiler as you're entering and leaving every function. That way you'd be able to keep an eye on how many cycles it took each function to complete. Independent of time.

That is extremely powerful, and not to be underestimated. A good solution would allow aggregate stats, so the total time in a function would be counted, including inside called functions.
markandrewslade at dontspamemeat dot gmail 13-Feb-2009 06:06
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.
anotheruser at example dot com 24-Aug-2008 01:50
Code evaluation script which uses debug_backtrace() to get execution time in ns, relative current line number, function, file, and calling function info on each tick, and shove it all in $script_stats array.  See debug_backtrace manual to customize what info is collected.

Warning: this will exhaust allowed memory very easily, so adjust tick counter according to the size of your code.  Also, array_key_exists checking on debug_backtrace arrays is removed here only to keep this example simple, but should be added to avoid a large number of resulting PHP Notice errors.

<?php

$script_stats
= array();
$time = microtime(true);

function
track_stats(){
    global
$script_stats,$time;
   
$trace = debug_backtrace();
   
$exe_time = (microtime(true) - $time) * 1000;
   
$func_args = implode(", ",$trace[1]["args"]);
   
$script_stats[] = array(
       
"current_time" => microtime(true),
       
"memory" => memory_get_usage(true),
       
"file" => $trace[1]["file"].': '.$trace[1]["line"],
       
"function" => $trace[1]["function"].'('.$func_args.')',
       
"called_by" => $trace[2]["function"].' in '.$trace[2]["file"].': '.$trace[2]["line"],
       
"ns" => $exe_time
       
);
   
$time = microtime(true);
    }

declare(
ticks = 1);
register_tick_function("track_stats");

// the rest of your project code

// output $script_stats into a html table or something

?>
zabmilenko at charter dot net 08-Jan-2008 10:49
If you misspell the directive, you won't get any error or warning.  The declare block will simply act as a nest for statements:

<?php
declare(tocks="four hundred")
{
   
// Has no affect on code and produces
    // no error or warning.
}
?>

Tested in php 5.2.5 on XPsp2
aeolianmeson at NOSPAM dot blitzeclipse dot com 30-May-2006 09: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:

<?php
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.
chris-at-free-source.com 28-Feb-2005 09: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 03: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 01-Feb-2003 08: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); }
rob_spamsux at rauchmedien dot ihatespam dot com 19-Mar-2002 11: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.