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 TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
list(var_1, var_2, ...)
The syntax flow is in a way where there is a list as function comprising of arguments passed from the function:
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.
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 :
Given below are the examples of PHP list:
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>
Output:
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>
Output:
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>
Output:
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>
Output:
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!