PHP Doku:: declare - control-structures.declare.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzKontrollstrukturendeclare

Ein Service von Reinhard Neidl - Webprogrammierung.

Kontrollstrukturen

<<switch

return>>

declare

Das declare-Konstrukt wird verwendet, um Ausführungsdirektiven für einen Codeblock festzulegen. Die Syntax von declare ist ähnliche wie die Syntax anderer Ablauf-Kontrollstrukturen:

declare (directive)
    statement

Die directive-Sektion erlaubt es, das Verhalten des declare-Blocks anzugeben. Aktuell werden zwei Direktiven unterstützt: die ticks-Direktive (siehe unten für weitere Informationen über die ticks-Direktive) und die encoding-Direktive (siehe unten für weitere Informationen über die encoding-Direktive).

Hinweis: Die Encoding-Direktive wurde in PHP 5.3.0 hinzugefügt.

Der statement-Teil des declare-Blocks wird ausgeführt - wie genau die Ausführung aussieht und welche Seiteneffekte während der Ausführung auftreten können, ist abhängig von der im directive-Block gesetzten Direktive.

Das declare-Konstrukt kann außerdem im globalen Sichtbarkeitsbereich verwendet werden, es hat dann Auswirkungen auf den gesamten folgenden Code (wird die Datei mit der declare-Anweisung inkludiert, hat die Anweisung jedoch keine Auswirkung auf das einbindende File).

<?php
// dies sind gleichwertige Schreibweisen:

// Sie können diese Schreibweise verwenden:
declare(ticks=1) {
    
// hier das vollständige Skript einfügen
}

// oder diese:
declare(ticks=1);
// hier das vollständige Skript einfügen
?>

Ticks

Achtung

Mit PHP 5.3.0 gilt Ticks als veraltet und wird mit PHP 6.0.0 entfernt.

Ein Tick ist ein Event, das alle N Low-Level-Statements auftritt, die vom Parser innerhalb des declare-Blocks ausgeführt werden. Der Wert für N wird durch die Verwendung von ticks=N innerhalb der directive-Sektion des declare-Blocks angegeben.

Das/die bei jedem Tick auftretenden Event(s) werden durch die Verwendung der Funktion register_tick_function() angegeben. Betrachten Sie das folgende Beispiel für mehr Details. Beachten Sie, dass mehr als ein Event bei jedem Tick auftreten kann.

Beispiel #1 Eine PHP-Codesektion profilen

<?php
// Eine Funktion, die die Zeit aufzeichnet, zu der sie aufgerufen wurde
function profile($dump FALSE)
{
    static 
$profile;

    
// Gibt die im Profil gespeicherten Zeiten zurück und löscht sie danach
    
if ($dump) {
        
$temp $profile;
        unset(
$profile);
        return 
$temp;
    }

    
$profile[] = microtime();
}

// Einen Tick-Handler angeben
register_tick_function("profile");

// Die Funktion vor dem declare-Block initialisieren
profile();

// Einen Codeblock ausführen, bei jedem zweiten Statement einen Tick auslösen
declare(ticks=2) {
    for (
$x 1$x 50; ++$x) {
        echo 
similar_text(md5($x), md5($x*$x)), "<br />;";
    }
}

// Die im Profiler gespeicherten Daten anzeigen
print_r(profile(TRUE));
?>
Das Beispiel analysiert den PHP-Code im 'declare'-Block und erfasst die Zeiten, an denen jedes zweite Low-Level-Statement innerhalb des Blocks ausgeführt wurde. Diese Information kann danach verwendet werden, um die langsamen Bereiche eines bestimmten Codesegments ausfindig zu machen. Dieser Prozess kann auch mit anderen Methoden ausgeführt werden - die Verwendung von Ticks ist einfacher und leichter zu implementieren.

Ticks sind sehr geeignet für das Debugging, die Implementierung einfachen Multitaskings, I/O-Verarbeitung im Hintergrund und viele weitere Anwendungen.

Siehe auch register_tick_function() und unregister_tick_function().

Encoding

Das Encoding eines Skripts kann pro Skript mittels der Encoding-Direktive festgelegt werden.

Beispiel #2 Das Encoding eines Skripts deklarieren.

<?php
declare(encoding='ISO-8859-1');
// hier folgt der Code
?>

Achtung

Die einzig zulässige Syntax für ein declare, das mit Namespaces kombiniert wird, ist declare(encoding='...');, wobei ... der encodete Wert ist. declare(encoding='...') {} bewirkt einen Parse-Error, wenn es mit Namespaces kombiniert wird.

Der encodete declare-Wert wird in PHP 5.3 ignoriert, sofern PHP nicht mit --enable-zend-multibyte kompiliert wurde. In PHP 6.0.0 wird die encoding-Direktive verwendet, um den Scanner darüber zu informieren, mit welchem Encoding die Datei erstellt wurde. Zulässige Werte sind Encodingbezeichnungen wie UTF-8.


15 BenutzerBeiträge:
- Beiträge aktualisieren...
ramamneh at gmail dot com
19.12.2010 20:15
check loaded server connection

<?php
$connection 
= false;
function
checkConnection( $connectionWaitingTime = 3 )
{
   
// check connection & time
   
global $time,$connection;
    if( (
$t = (time() - $time)) >= $waitingTime  && !$connection){ 
        echo (
"<p> Server not responding  for <strong>$t</strong> seconds !! </p>");
        die(
"Connection aborted");
           
    }
   
}

register_tick_function("checkConnection");
$time = time();
declare (
ticks=1)
{
    while(
true ){ // connecting to loaded server
   
}
   
$connection = true ;
}
?>
Anonymous
27.09.2010 23: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.02.2009 18: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.08.2008 1: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
8.01.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
rsemil at gmail dot com
6.01.2008 15: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
rosen_ivanov at abv dot bg
28.08.2006 15: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 :)
aeolianmeson at NOSPAM dot blitzeclipse dot com
30.05.2006 9: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.
warhog at warhog dot net
18.12.2005 21: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
}

?>
chris-at-free-source.com
28.02.2005 21: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
8.07.2003 3: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
1.02.2003 20: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
8.01.2003 11: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.03.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.
rob_spamsux at rauchmedien dot ihatespam dot com
19.03.2002 10: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)