PHP 목록

WBOY
풀어 주다: 2024-08-29 12:48:16
원래의
1169명이 탐색했습니다.

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, ...)
로그인 후 복사

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>
로그인 후 복사

Output:

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>
로그인 후 복사

Output:

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>
로그인 후 복사

Output:

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>
로그인 후 복사

Output:

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.

위 내용은 PHP 목록의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿