You get a bonus - 1 coin for daily activity. Now you have 1 coin

PHP declaration constructs declaration declare directive tick

Lecture



The declare declaration construct is used to set the execution directives for a block of code. The declare syntax is similar to the syntax of other PHP control constructs:

declare (директива)
инструкция;

The directive allows you to set the behavior of the declare block. Currently, only one directive is available in PHP - tick . The instruction is part of the declare block.

How the instruction (s) will be executed depends on the directive.

The declare construct can be used in a global scope, affecting all the code after it.

<?php
// Вы можете равнозначно использовать следующие методы

// Так:
declare(ticks=1) {
// Здесь полный сценарий
}

// Или так:
declare(ticks=1);
// Здесь полный сценарий
?>

Tick ​​directive

tick is an event that occurs for every lower-level N-instructions executed by the parser within the declare block. Events occurring on each tick are determined by the register_tick_function () function.

declare

The declare construct is used to set the execution directives for a block of code. The declare syntax is similar to the syntax of other flow control constructs:

  declare (directive) statement 

The directive section allows you to set the declare block behavior. Currently only one directive is recognized: ticks . (See further about the ticks directive.)

The statement part of the declare block will be executed — how it is executed and which side effects occur during execution depends on the directive set in the directive block.

Ticks / Tiki

A tick / tick is an event that occurs for each lower-level operator N performed by the parser inside the declare block. The value of N is specified by ticks = N inside the directive section of the declare block.

The event (s) that occurs with each tick is specified by the register_tick_function () function. See details further in the example. Note that more than one event may occur for each tick.

Example 11-1. Profiling the PHP code section

  <? php
 // A function that records the time when it is called
 function profile ($ dump = FALSE)
 {
     static $ profile;

     // Returns the time stored in the profile, then deleted
     if ($ dump) {
         $ temp = $ profile;
         unset ($ profile);
         return ($ temp);
     }

     $ profile [] = microtime ();
 }

 // Set the tick handler / tick handler
 register_tick_function ("profile");

 // Initialize the function before the declare block
 profile ();

 // The code block is executed, the tick is called on every second operator
 declare (ticks = 2) {
     for ($ x = 1; $ x <50; ++ $ x) {
         echo similar_text (md5 ($ x), md5 ($ x * $ x)), "<br />;";
     }
 }

 // Displays the data stored in the profiler
 print_r (profile (TRUE));
 ?> 

This example profiles the PHP code inside the 'declare' block, recording the time when each second low-level statement of the executed block is executed. This information can then be used to search for slow sections of a specific code segment. This process can be performed using other methods: the use of ticks is more convenient and easier to implement.

Ticks are well suited for debugging, implementing simple multitasking, background I / O, and many other tasks.

See also register_tick_function () and unregister_tick_function () .


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)