Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

PHP declare statement

In PHP, declare is a constructor, and its usage differs greatly from the control structures we are accustomed to. This constructor is used to set execution directives for a block of code.

But what do we mean by this?

If you have read about CSP in PHP, you may get an idea of how this constructor works.

But let’s go to what a directive is in our language.

A directive is an instruction or guideline given to someone to follow a specific course of action; in short, we are telling our block something it should do when executed.

Usable Directives in declare statement

In the declare constructor, we have 3 usable directives: ticks, strict_type, and encoding.

ticks

https://blastcoding.com/en/php-declare-statement/#ticks

This directive allows executing a specific function after a certain number of low-level statements.

Events that occur on each tick are specified using the register_tick_function() function.

Description / Descripción
register_tick_function(callable $function, mixed $arg = ?, mixed $... = ?): bool
One thing to keep in mind is that register_tick_function() also generates a tick.
Ejemplo de el uso de ticks
register_tick_function("miFuncion");// es una sentencia

function miFuncion() {
  echo "Se ha ejecutado la función miFuncion \n";
};
declare(ticks=2){
	for ($i = 0; $i < 5;$i++) {
	  echo "Iteración $i \n";// es una sentencia
	  echo "Iteración2 $i \n";// es una sentencia
	}
}
Iteración 0 
Iteración2 0 
Se ha ejecutado la función miFuncion 
Iteración 1 
Iteración2 1 
Se ha ejecutado la función miFuncion 
Iteración 2 
Iteración2 2 
Se ha ejecutado la función miFuncion 
Iteración 3 
Iteración2 3 
Se ha ejecutado la función miFuncion 
Iteración 4 
Iteración2 4 
Se ha ejecutado la función miFuncion 

To understand ticks, you can think of it like a clock. Every second, a tick is generated. However, here, each tickable basic statement generates a tick.

Understanding the ticks
register_tick_function("miFuncion");

function miFuncion() {
  echo "Se ha ejecutado la función miFuncion \n";
};
declare(ticks=1){
	for ($i = 0; $i < 2;$i++) {
	  echo "Iteración $i \n";
	  echo "Iteración2 $i \n";
	}
}
Iteración 0 
Se ha ejecutado la función miFuncion 
Iteración2 0 
Se ha ejecutado la función miFuncion 
Iteración 1 
Se ha ejecutado la función miFuncion 
Iteración2 1 
Se ha ejecutado la función miFuncion 
Se ha ejecutado la función miFuncion 

In this example, you can see that in its result we see one last "The function miFuncion has been executed" too many. This is because register_tick_function("miFuncion"); is generating this last tick and it is doing it outside of our loop.

The use of versions >= PHP 8 is recommended for the following directives.

strict_types

https://blastcoding.com/en/php-declare-statement/#strict_types

strict_type only can take 2 values:

  • 0: Disables strict typing. This is the default behavior of PHP.
  • 1: Enables strict typing.

What is strict typing?

This means that the data type of each variable and expression must be compatible with the context in which it is used. Do I think we already saw this in functions in PHP?

Con declare(strict_type=1)
<?php

declare(strict_types=1);

function holamundo(string $a, string $b){
    echo $a." ".$b;
}
holamundo(1,"mundo");
Fatal error: Uncaught TypeError: holamundo(): Argument #1 ($a) must be of type string, int given, called in C:\xampp\htdocs\tutorialphp\funciones.php on line 6 and defined in C:\xampp\htdocs\tutorialphp\funciones.php:3 Stack trace: #0 C:\xampp\htdocs\tutorialphp\funciones.php(6): holamundo(1, 'mundo') #1 {main} thrown in C:\xampp\htdocs\tutorialphp\funciones.php on line 3

See that if or if $a must be a string and therefore it gives us a TypeError, since holamundo is receiving a parameter that is an integer.

Encoding

https://blastcoding.com/en/php-declare-statement/#encoding

With declare you can also specify the encoding that you want to use in the script.

<?php
declare(encoding='ISO-8859-1');
// código aquí
?>
These are the most used encoding types, if you are unsure of what encode to use, use UTF-8

UTF-8: The default Unicode encoding in PHP, compatible with most characters in the world.

ISO-8859-1: An 8-bit character encoding for Western European languages.

ASCII: A subset of ISO-8859-1 that is compatible with basic English characters.

UTF-16: A Unicode encoding that uses 16 bits per character.

UTF-32: A Unicode encoding that uses 32 bits per character.

Windows-1252: An 8-bit character encoding for Western and Central European languages.

ISO-2022-JP: An 8-bit character encoding for Japanese.

EUC-JP: A multi-byte character encoding for Japanese.

GB2312: A multi-byte character encoding for simplified Chinese.

Big5: A multi-byte character encoding for traditional Chinese.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments