declare

declare구문은 코드 블록의 수행 지시어를 활성화하기 위해 사용된다. declare문법은 다른 흐름 제어 구조의 문법과 비슷하다:

declare (directive) statement

directive 부분은 declare 블록의 동작을 활성화합니다. 현재는 두 지시어만 인식합니다: ticks 지시어(자세한 정보는 아래 ticks 지시어 참고)와 encoding 지시어(자세한 정보는 아래 encoding 지시어 참고).

Note: encoding 지시어는 PHP 5.3.0에서 추가되었습니다.

declare블록의 statement부분이 수행될것이다 - 어떻게 수행이 되고 수행중에 어떤 사이드 이펙트가 발생할지는 directive블록의 디렉티브에 달려있다.

declare 구문은 전역 유효영역 안에서 사용할수 있다. 그래서 모든 코드가 그 디렉티브에 영향을 받는다. (그러나 declare가 있는 파일을 포함하였을 때에는 원 파일에는 영향을 주지 않습니다)

<?php
// 이들은 동일합니다:

// 이를 사용할 수 있습니다:
declare(ticks=1) {
    
// 여기에 전체 스크립트
}

// 또는 이렇게 사용할 수 있습니다:
declare(ticks=1);
// 여기에 전체 스크립트
?>

틱(Ticks)

Caution

PHP 5.3.0부터 틱은 배제되었고, PHP 6.0.0에서 제거될 예정입니다.

틱은 declare블록에서 파서에 의해 수행되는 N 저레벨(low-level) 구문마다 발생하는 이벤트이다. N 값은 declare블록의 directive부분에서 ticks=N 으로 지정할수 있다.

각 틱에서 발생하는 이벤트(들)은 register_tick_function()함수 를 써서 지정한다. 자세한 것은 아래 예제를 볼것. 각 틱에서는 하나 이상의 이벤트가 발생할수 있음에 주의해야 한다.

Example #1 PHP 각 코드 섹션의 분석표만들기(Profile)

<?php
// 호출될대의 시간을 기록하는 함수
function profile($dump FALSE)
{
    static 
$profile;

    
// Profile에 저장된 모든 시간 리턴하고, 삭제함
    
if ($dump) {
        
$temp $profile;
        unset(
$profile);
        return 
$temp;
    }

    
$profile[] = microtime();
}

// 틱 핸들러 설정
register_tick_function("profile");

// declare 블록 전에 함수를 초기화
profile();

// 코드 블록의 실행하고, 두번째 구문에 틱을 부여함
declare(ticks=2) {
    for (
$x 1$x 50; ++$x) {
        echo 
similar_text(md5($x), md5($x*$x)), "<br />;";
    }
}

// 분석표에 저장된 데이터를 출력
print_r(profile(TRUE));
?>
위 예제 코드는 실행 블록안의 두번째 저레벨(low-level) 구문의 시간에 따라 'declare'블록 안의 PHP코드를 분석한다. 이런 정보로 어느 코드 부분에서 느려지는지 알아볼 수 있다. 이런 처리방법은 다른 기법으로 수행할수있다: 틱을 이용하는 것은 좀더 편하고 좀더 구현하기 쉽다.

틱은 디버깅, 단순한 멀티태스킹 구현, 백그라운드 I/O와 다른 많은 작업 에 적합하게 이용할수 있다.

register_tick_function()함수와 unregister_tick_function()함수를 참고하세요.

인코딩

encoding 지시어를 사용하여 스크립트 별로 지정할 수 있는 인코딩입니다.

Example #2 스크립트의 인코딩 선언하기

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

Caution

이름공간과 결합할 때, declare의 적합한 문법은 declare(encoding='...'); 뿐입니다. (...은 인코딩 값) 이름공간과 결합했을 때, declare(encoding='...') {}은 해석 오류를 발생합니다.

PHP 5.3에서는 PHP가 --enable-zend-multibyte로 컴파일 되지 않았을 경우, encoding 선언 값이 무시됩니다. PHP 6.0에서는, encoding 지시어가 파일이 생성된 인코딩을 스캐너에게 알려줍니다. 적절한 값은 UTF-8 등의 인코딩 이름입니다.

add a note add a note

User Contributed Notes 15 notes

up
52
Anonymous
13 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
7
digitalaudiorock at gmail dot com
5 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
20
Kubo2
9 years ago
Note that in PHP 7 <?php declare(encoding='...'); ?> throws an E_WARNING if Zend Multibyte is turned off.
up
18
sawyerrken at gmail dot com
10 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
2
digitalaudiorock at gmail dot com
5 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
3
ohcc at 163 dot com
4 years ago
It's possible to set directives at one time if every directive is supported.
<?php
   
declare(strict_types=1, encoding='UTF-8');
?>
up
4
php dot net at e-z dot name
10 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
2
ja2016 at wir dot pl
6 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
2
markandrewslade at dontspamemeat dot gmail
15 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
2
fok at nho dot com dot br
20 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
0
ohcc at 163 dot com
5 years ago
Using a directive that is not supported by current version of PHP will generate a warning like:

Warning: Unsupported declare 'strict_types' in D:\Server\wuxiancheng.cn\index.php on line 2

You CAN'T prepend an @ sign to the declare construct to supress that error message.

@declare(strict_types=1);

The above code will lead to a Parse error like:

Parse error: syntax error, unexpected 'declare' (T_DECLARE) in D:\Server\wuxiancheng.cn\index.php on line 2
up
-1
aeolianmeson at NOSPAM dot blitzeclipse dot com
17 years ago
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.
up
-6
php at niekbosch dot nl
10 years ago
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ß
up
-2
wapmorgan at gmail dot com
6 years ago
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 ..
}
up
-9
Ionut
7 years ago
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 [...]
?>
To Top