declare

(PHP 4, PHP 5, PHP 7, PHP 8)

ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡ declare устанавливаСт Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ выполнСния для Π±Π»ΠΎΠΊΠ° ΠΊΠΎΠ΄Π°. Бинтаксис конструкции declare Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π΅Π½ синтаксису Π΄Ρ€ΡƒΠ³ΠΈΡ… конструкций управлСния ΠΏΠΎΡ‚ΠΎΠΊΠΎΠΌ:

declare (directive)
    statement

Π§Π°ΡΡ‚ΡŒ directive Π·Π°Π΄Π°Ρ‘Ρ‚ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π±Π»ΠΎΠΊΠ° declare. PHP ΠΏΠΎΠΊΠ° распознаёт Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‚Ρ€ΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹:

Π”ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°ΡŽΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π»ΠΈΡ‚Π΅Ρ€Π°Π»ΡŒΠ½Ρ‹Π΅ значСния, ΠΏΠΎΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽΡ‚ΡΡ ΠΏΡ€ΠΈ компиляции Ρ„Π°ΠΉΠ»Π°. НСльзя ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΈ константы. ΠŸΡ€ΠΈΠΌΠ΅Ρ€:

<?php

// ΠŸΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ:
declare(ticks=1);

// НСдопустимо:
const TICK_VALUE = 1;
declare(
ticks=TICK_VALUE);

PHP Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ Ρ‡Π°ΡΡ‚ΡŒ statement Π±Π»ΠΎΠΊΠ° declare β€” ΠΊΠ°ΠΊ PHP Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ инструкции ΠΈ ΠΊΠ°ΠΊΠΈΠ΅ ΠΏΠΎΠ±ΠΎΡ‡Π½Ρ‹Π΅ эффСкты Π²ΠΎΠ·Π½ΠΈΠΊΠ°ΡŽΡ‚ Π²ΠΎ врСмя выполнСния, зависит ΠΎΡ‚ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ установили Π² Π±Π»ΠΎΠΊΠ΅ directive.

ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡŽ declare Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ ΠΎΠ±ΡŠΡΠ²Π»ΡΡ‚ΡŒ Π² глобальной области видимости, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ²Π»ΠΈΡΡ‚ΡŒ Π½Π° вСсь ΠΊΠΎΠ΄ Π·Π° Π½Π΅ΠΉ (ΠΎΠ΄Π½Π°ΠΊΠΎ, Ссли Ρ„Π°ΠΉΠ» с объявлСниСм конструкции declare Π²ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ Π² Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ Ρ„Π°ΠΉΠ», конструкция Π½Π΅ повлияСт Π½Π° Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ Ρ„Π°ΠΉΠ»).

<?php

// Π­Ρ‚ΠΎ Ρ‚ΠΎ ΠΆΠ΅ самоС:

// ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡŽ declare ΠΎΠ±ΡŠΡΠ²Π»ΡΡŽΡ‚ Π²ΠΎΡ‚ Ρ‚Π°ΠΊ:
declare(ticks=1) {
// Π‘ΠΊΡ€ΠΈΠΏΡ‚ записываСтся Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° с Ρ„ΠΈΠ³ΡƒΡ€Π½Ρ‹ΠΌΠΈ скобками
}

// ΠΈΠ»ΠΈ Ρ‚Π°ΠΊ:
declare(ticks=1);
// Π‘ΠΊΡ€ΠΈΠΏΡ‚ записываСтся послС объявлСния конструкции declare

Π’ΠΈΠΊΠΈ

Π’ΠΈΠΊ β€” событиС, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ‚ ΠΊΠ°ΠΆΠ΄Ρ‹Π΅ N Π½ΠΈΠ·ΠΊΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹Ρ… инструкций, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ парсСр ΠΎΡ‚ΠΌΠ΅Ρ‡Π°Π΅Ρ‚ ΠΊΠ°ΠΊ Ρ‚Π°ΠΊΡ‚ ΠΈ выполняСт Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° declare. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ N ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ ΠΊΠ°ΠΊ ticks=N Π²Π½ΡƒΡ‚Ρ€ΠΈ части directive Π±Π»ΠΎΠΊΠ° declare.

ΠŸΠ°Ρ€ΡΠ΅Ρ€ ΠΎΡ‚ΠΌΠ΅Ρ‡Π°Π΅Ρ‚ ΠΊΠ°ΠΊ Ρ‚Π°ΠΊΡ‚ Π½Π΅ ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ΠΈΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡŽ. ΠžΠ±Ρ‹Ρ‡Π½ΠΎ условныС выраТСния ΠΈ выраТСния Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² Π½Π΅ ΠΏΠΎΠ΄ΡΡ‡ΠΈΡ‚Ρ‹Π²Π°ΡŽΡ‚ΡΡ.

Бобытия, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ°ΡŽΡ‚ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ Ρ‚ΠΈΠΊΠ΅, ΠΏΠΎΠΌΠΎΠ³Π°Π΅Ρ‚ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ функция register_tick_function(). ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½ΠΎΡΡ‚ΠΈ содСрТит ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Ρ‚ΠΈΠΊΠ° ΠΈΠ½ΠΎΠ³Π΄Π° Π²ΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ‚ большС ΠΎΠ΄Π½ΠΎΠ³ΠΎ события.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Ρ‚ΠΈΠΊΠ°

<?php

declare(ticks=1);

// Ѐункция, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ PHP Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΌ Ρ‚ΠΈΠΊΠ΅
function tick_handler()
{
echo
"PHP Π²Ρ‹Π·Π²Π°Π» Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ tick_handler()\n";
}

register_tick_function('tick_handler'); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС Ρ‚ΠΈΠΊΠ°

$a = 1; // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС Ρ‚ΠΈΠΊΠ°

if ($a > 0) {
$a += 2; // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС Ρ‚ΠΈΠΊΠ°
print $a; // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС Ρ‚ΠΈΠΊΠ°
}

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅ описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ register_tick_function() ΠΈ unregister_tick_function().

ΠšΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°

Π”ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Π° encoding ΡƒΠΌΠ΅Π΅Ρ‚ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΡƒ скрипта для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ скрипта.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ОбъявлСниС ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠΈ скрипта

<?php

declare(encoding='ISO-8859-1');
// Π’ этом мСстС ΠΈΠ΄Ρ‘Ρ‚ ΠΊΠΎΠ΄
ΠŸΡ€Π΅Π΄ΠΎΡΡ‚Π΅Ρ€Π΅ΠΆΠ΅Π½ΠΈΠ΅

ЕдинствСнный допустимый синтаксис для конструкции declare Π² сочСтании с пространствами ΠΈΠΌΡ‘Π½ выглядит Π²ΠΎΡ‚ Ρ‚Π°ΠΊ: declare(encoding='...'); Π³Π΄Π΅ ... β€” Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠΈ. Π’ сочСтании с пространствами ΠΈΠΌΡ‘Π½ запись declare(encoding='...') {} Π²Ρ‹Π·ΠΎΠ²Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ парсСра.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅ описаниС Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ zend.script_encoding.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 10 notes

up
71
Anonymous ΒΆ
15 years ago
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.
up
20
Kubo2 ΒΆ
11 years ago
Note that in PHP 7 <?php declare(encoding='...'); ?> throws an E_WARNING if Zend Multibyte is turned off.
up
6
digitalaudiorock at gmail dot com ΒΆ
7 years ago
Regarding my previous comment as to the change in scope of declare(ticks=1) between 5.6 and 7.x, I intended to mention another example of the affect this can have on signal handlers:

If your script uses declare(ticks=1) and assigns handlers, in 5.6 signals will get caught and call the handler even when the code that is running is in an included file (where the included file doesn't have the declaration). However in 7.x the signal wouldn't get caught until the code returns to the main script.

The best solution to that is to use pcntl_async_signals(true) when it's available, which will allow the signals to get caught regardless of what file the code happens to be in.
up
22
sawyerrken at gmail dot com ΒΆ
12 years ago
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.
up
9
php dot net at e-z dot name ΒΆ
12 years ago
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
up
8
digitalaudiorock at gmail dot com ΒΆ
7 years ago
A few important things to note for anyone using this in conjunction with signal handlers:

If anyone is trying to optionally use either pcntl_async_signals() when available (PHP >= 7.1) or ticks for older versions, this is not possible...at least not in a way that does NOT enable ticks for newer PHP versions. This is because there is simply no way to conditionally declare ticks. For example, the following will "work" but not in the way you might expect:

<?php
if (function_exists('pcntl_async_signals')) {
    pcntl_async_signals(true);
} else {
    declare(ticks=1);
}
?>

While signal handlers will work with this for old and new version, ticks WILL be enabled even in the case where pcntl_async_signals exists, simply because the declare statement exists. So the above is functionally equivalent to:

<?php
if (function_exists('pcntl_async_signals')) pcntl_async_signals(true);
declare(ticks=1);
?>

Another thing to be aware of is that the scoping of this declaration changed a bit from PHP 5.6 to 7.x...actually it was corrected apparently as noted here:

http://php.net/manual/en/function.register-tick-function.php#121204

This can cause some very confusing behavior. One example is with the pear/System_Daemon module. With PHP 5.6 that will work with a SIGTERM handler even if the script using it doesn't itself use declare(ticks=1), but does not work in PHP 7 unless the script itself has the declaration. Not only does the handler not get called, but the signal does nothing at all, and the script doesn't exit.

A side note regarding ticks that's annoyed me for some time: As if there wasn't enough confusion around all this, the Internet is full of false rumors that ticks were deprecated and are being removed, and I believe they all started here:

http://www.hackingwithphp.com/4/21/0/the-declare-function-and-ticks

Despite a very obscure author's note at the very end of the page saying he got that wrong (that even I just noticed), the first very prominent sentence of the article still says this, and that page is near the top of any Google search.
up
6
ja2016 at wir dot pl ΒΆ
8 years ago
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
up
5
markandrewslade at dontspamemeat dot gmail ΒΆ
17 years ago
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.
up
4
fok at nho dot com dot br ΒΆ
22 years ago
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. =--
up
-1
ohcc at 163 dot com ΒΆ
6 years ago
It's possible to set directives at one time if every directive is supported.
<?php
    declare(strict_types=1, encoding='UTF-8');
?>