Blog de programación, errores, soluciones

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

PHP list constructor

PHP list assign variables as if they were an array, at least that’s the definition on official page. I would say it assigns the elements of the array to variables, variables that will be created when we pass them as parameters to list.

list() only works with numeric arrays and assumes that numeric indexes start at 0.
We can already see that the concept of list itself is not very clear, and furthermore, we must add that list assigns values to variables differently depending on the version of PHP we are using. This is highlighted in the documentation with the following warning:

Warning

In PHP 5, `list()` assigns values starting from the rightmost parameter. In PHP 7, `list()` starts from the leftmost parameter.

If I were to recommend something, it would be not to use PHP `list` if you are on a version 5.x because in the future, if the server version is changed, it can lead to unexpected behaviors.

Let’s first look at its description and syntax, and then we can conduct some tests to have a clearer understanding of the concept.

Description / Descripción
list(mixed $var1, mixed ...$... = ?): array
Sintaxis / Sintax
list($variable1,$variable2) =$mi_array;

Examples

Let’s start with a simple example that also uses the array_values function.

Ejemplo de blastcoding.com
$caracteristicas_auto = [
    "marca" => "Toyota",
    "modelo" => "Camry",
    "año" => 2022,
    "color" => "Azul",
    "motor" => "2.5L",
    "tracción" => "Delantera"
];

list($marca, $modelo, $año, $color, $motor, $traccion) = array_values($caracteristicas_auto);

echo "Marca: " . $marca . "<br>";
echo "Modelo: " . $modelo . "<br>";
echo "Año: " . $año . "<br>";
echo "Color: " . $color . "<br>";
echo "Motor: " . $motor . "<br>";
echo "Tracción: " . $traccion . "<br>";
Marca: Toyota
Modelo: Camry
Año: 2022
Color: Azul
Motor: 2.5L
Tracción: Delantera

Let’s look at another example to explore more features of list.

Ejemplo sacado de php.net
$info = array('café', 'marrón', 'cafeína');

// Enumerar todas las variables
list($bebida, $color, $energía) = $info;
echo "El $bebida es $color y la $energía lo hace especial.\n";

// Enumerar algunas de ellas
list($bebida, , $energía) = $info;
echo "El $bebida tiene $energía.\n";

// U omitir solo la tercera
list( , , $energía) = $info;
echo "Necesito $energía!\n";

// list() no funciona con cadenas
list($bar) = "abcde";
var_dump($bar); // NULL
El café es marrón y la cafeína lo hace especial.
El café tiene cafeína.
Necesito cafeína!
NULL

The list function can be used in conjunction with Heredoc, as you can create a list to then display on the screen in a clear way, thus helping the readability of the code.

Reference: https://www.php.net/manual/en/function.list.php

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