Liste PHP

WBOY
Libérer: 2024-08-29 12:48:16
original
1104 Les gens l'ont consulté

PHP list function is an important function used for assigning values to a list of variables while performing single operation at one go. This function is not present in all the PHP versions and is introduced exclusively in PHP versions before 7.1 which works only for numerical arrays while assigning values to the list of variables. The values assigned to the variables is the return type once executed with the help of PHP list function. PHP list function is not actually a function like array rather it is considered a language construct for assigning values.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

list(var_1, var_2, ...)
Copier après la connexion

The syntax flow is in a way where there is a list as function comprising of arguments passed from the function:

  • list: The function list() is declared.
  • var_1: The variable passed as an argument is required and is quite of mandatory in the sense this acts as the first variable to assign a value to the variable declared.
  • var_2: The second variable is optional and then this variable is used to assign values to the list followed by sequence.

This syntax when applied has a return type as assigned array which means whatever values are assigned to the array is the return type for that instance.

How list Function works in PHP?

list() function is an inbuild function in PHP which is exclusively used for assigning values to more than one variable by performing a single operation at the time of execution.

Let’s see the actual flow for working of list function in PHP which is described as follows :

  • Initially list being an inbuild function doesn’t required to be written and doesn’t require any external function call it works just seamlessly without much intrusion.
  • The array gets assigned with the required values from the multiple values considered at the time of execution.
  • There is a misconception regarding the array declared as a variable for assigning values but it’s just a myth in actual it is just a language construct.
  • Everything gets executed in a single operation using list() function while assigning variable.
  • This function works seamlessly on the numerical arrays only, which represents the fact that the user will get interacted with the arrays using first variable which is var_1.
  • The second argument getting passed to the function is an optional argument which gets retrieved and worked once the first argument satisfies the condition.
  • One point needs to be kept in mind which is like the number of variables should not exceed length of the numerical array and in case it exceeds the array defined variable then it will give error with parameters types and there will be no return type at the time of execution.
  • There should be no exception introduced while executing this list function otherwise the requirement and the return type will not get suffice.
  • If a function is not having any return statement, then implicitly it will return NULL as its return type at the time of execution.
  • The parameters to be passed as part of the function should be arranged in a way where the list of variables will get separated by spaces within the code.
  • The first variable to be passed from the function is a mandatory variable for the return type.
  • Another important point to look upon is the version compatibility which means that the PHP version should have version support less than 7.
  • Also, coming to the version compatibility for PHP then PHP version 5 in the list should assign values starting with right most parameter.
  • Whereas there is a difference with PHP version 7 where the assignment to the values of variable which will appear as left-most parameter.
  • In case normal variables are used then there is no need to worry about assigning values to the variables and then using these arrays with indices is used for arranging the values in an order.
  • But in case of order must be maintained like from left to right or from right to left then it is very much need to keep in mind about the PHP versioning.

Examples of PHP list

Given below are the examples of PHP list:

Example #1

This program demonstrates the PHP list where the array is assigned with the variable having values as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$an_arr = array("Banana","Mango","Apple");
list($a_1, $b_2, $c_3) = $an_arr;
echo "I have many fruits, one $a_1, one $b_2 and one $c_3.";
?>
</body>
</html>
Copier après la connexion

Output:

Liste PHP

Example #2

This program demonstrates the PHP list where array is assigned with the first and third value within the variable as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$arr_b = array("Pencil","Copy","Pen");
list($k_0, , $z_1) = $arr_b;
echo "Used_variable_for_modification $k_0 and $z_1 variables.";
?>
</body>
</html>
Copier après la connexion

Output:

Liste PHP

Example #3

This program demonstrates the declaration of array in a way where initially all the variables are listed, followed by retrieving some values and then listing some of them among all from which the third one gets skipped in case of the list with all the string it will return the NULL value as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$in_p = array('choco', 'caramel', 'pancake');
list($choco, $cake, $caramel) = $in_p;
echo "$choco cake $color and $caramel appears relishing \n";
list( , , $caramel) = $in_p;
echo "caramel cake tastes wow $caramel!\n";
list($choco, , $cake) = $in_p;
echo "$choco has $cake.\n";
list($gi_t) = "lost_in \n";
list($choco, , $cake) = $in_p;
echo "$choco has $cake.\n";
var_dump($gi_t);
?>
</body>
</html>
Copier après la connexion

Output:

Liste PHP

Example #4

This program demonstrates the nested array by using list function as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
list($a_1, list($b_2, $c_0)) = array(1, array(4, 6));
var_dump($a_1, $b_2, $c_0);
echo "listing of nested array";
?>
</body>
</html>
Copier après la connexion

Output:

Liste PHP

Conclusion

PHP list is an inbuild function which gives user the flexibility and versatility to adapt this function as part of the implementation as per requirement. Use of PHP list make the values arranged in some order which gives user visibility to implement a user-friendly array return values with variables.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
php
source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!