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
declare
declare구문은 코드 블록의 수행 디렉티브(directive)를 활성화하기 위해 사용된다. declare문법은 다른 흐름 제어 구조의 문법과 비슷하다:
declare (directive) statement
directive부분은 declare블록의 동작이 활성화되도록 해준다. 현재는 오직 한 디렉티브만 인식된다: ticks 디렉티브. (더 자세한 정보는 ticks 디렉티브를 볼것)
declare블록의 statement부분이 수행될것이다 - 어떻게 수행이 되고 수행중에 어떤 사이드 이펙트가 발생할지는 directive블록의 디렉티브에 달려있다.
declare 구문은 전역 유효영역 안에서 사용할수 있다. 그래서 모든 코드가 그 디렉티브에 영향을 받는다.
<?php
// 이들은 동일합니다:
// 이를 사용할 수 있습니다:
declare(ticks=1) {
// 여기에 전체 스크립트
}
// 또는 이렇게 사용할 수 있습니다:
declare(ticks=1);
// 여기에 전체 스크립트
?>
틱(Ticks)
틱은 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));
?>
틱은 디버깅, 단순한 멀티태스킹 구현, 백그라운드 I/O와 다른 많은 작업 에 적합하게 이용할수 있다.
register_tick_function()함수와 unregister_tick_function()함수를 참고하세요.
declare
08-Jan-2008 01:49
06-Jan-2008 06:30
rosen_ivanov's solution can be replaced by a simple call to memory_get_peak_usage() if you're running at least PHP 5.2.0
28-Aug-2006 06:06
As Chris already noted, ticks doesn't make your script multi-threaded, but they are still great. I use them mainly for profiling - for example, placing the following at the very beginning of the script allows you to monitor its memory usage:
<?php
function profiler($return=false) {
static $m=0;
if ($return) return "$m bytes";
if (($mem=memory_get_usage())>$m) $m = $mem;
}
register_tick_function('profiler');
declare(ticks=1);
/*
Your code here
*/
echo profiler(true);
?>
This approach is more accurate than calling memory_get_usage only in the end of the script. It has some performance overhead though :)
30-May-2006 12: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.
18-Dec-2005 12: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
}
?>
28-Feb-2005 12: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.
07-Jul-2003 06: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. =--
01-Feb-2003 11: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); }
08-Jan-2003 02: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)
19-Mar-2002 02: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.
19-Mar-2002 01: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.
