PHP – declare statement

The “declare” statement in PHP is used to set execution directives for a block of code in the program. The syntax of declare is  almost similar to other control constructs as given below:

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

  1. the ticks directive
  2. the encoding directive
  3. the strict_types directive

the ticks directive in PHP

A tick in declare statement is an event that occurs for every “n”  low-level tickable statements executed by the PHP parser within the declare statement block. The value of “n will be  specified using ticks=n within the declare directive section.

Example 1:

Example 2

the encoding directive in PHP

Any script’s encoding can be specified/declared as per-script using the encoding directive. You can understand this with the following example:

Example:

the strict_types directive in PHP

In this directive, a variable of exactly same type of the type declaration will be accepted otherwise a TypeError will be thrown. If you want to enable strict mode, then the declare statement is used with the strict_types declaration.

Example:

 

Leave a Reply

Your email address will not be published.