PHP.nl

declare

declare

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

`declare``declare````php declare (directive) statement





  The  section allows the
  behavior of the  block to
  be set.
  Currently only three directives are recognized:
  
 `directive``declare`ticksencodingstrict_types


  As directives are handled as the file is being compiled, only literals may
  be given as directive values. Variables and constants cannot be used. To
  illustrate:
  
 ```php
<?php
// This is valid:
declare(ticks=1);

// This is invalid:
const TICK_VALUE = 1;
declare(ticks=TICK_VALUE);
?>

The part of the block will be executed - how it is executed and what side effects occur during execution may depend on the directive set in the block. statement``declare``directive

The construct can also be used in the global scope, affecting all code following it (however if the file with was included then it does not affect the parent file).

`declare``declare````php




### Ticks

A tick is an event that occurs for every
   low-level tickable statements executed
  by the parser within the  block.
  The value for  is specified
  using 
  within the  block's
   section.
 `N``declare``N``ticks=N``declare``directive`

Not all statements are tickable. Typically, condition
  expressions and argument expressions are not tickable.


  The event(s) that occur on each tick are specified using the
  . See the example
  below for more details. Note that more than one event can occur
  for each tick.
 `register_tick_function`


  
 **Voorbeeld: Tick usage example**

```php
<?php

declare(ticks=1);

// A function called on each tick event
function tick_handler()
{
    echo "tick_handler() called\n";
}

register_tick_function('tick_handler'); // causes a tick event

$a = 1; // causes a tick event

if ($a > 0) {
    $a += 2; // causes a tick event
    print $a; // causes a tick event
}

?>

See also and . register_tick_function``unregister_tick_function

Encoding

A script's encoding can be specified per-script using the  directive.

encodingVoorbeeld: Declaring an encoding for the script

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

Let op: > When combined with namespaces, the only legal syntax for declare is where is the encoding value.
will result in a parse error when combined with namespaces. declare(encoding='...');``...``declare(encoding='...') {}

See also . zend.script_encoding