How to use php list function

青灯夜游
Release: 2023-02-22 20:58:02
Original
3289 people have browsed it

The list() function is a built-in function in PHP that is used to assign array values ​​to multiple variables at one time. This function only works with numeric arrays; when an array is assigned to multiple values, the first element in the array is assigned to the first variable, the second element to the second variable, and so on until until the end of the last variable.

How to use php list function

How to use the php list() function?

php The list() function is used to assign array values ​​to multiple variables at one time.

Syntax:

list(var1,var2...)
Copy after login

Parameters: The list() function accepts a space-separated list of variables; the first variable is mandatory.

●var1: required. The first variable to be assigned a value.

● var2,...: optional. More variables need to be assigned values.

Return value: The function returns an allocated array to the multiple variables passed. If m>n, it will not assign a value to the var variable list and will report an error, where n is the length of the array and m is the number of variables.

Note: The

list() function is only used for numerical arrays; when the array is assigned to multiple values, the first element in the array is assigned to The first variable, the second element is assigned to the second variable, and so on until the end of the last variable. The number of variables cannot exceed the length of the numeric array.

Let’s take a look at how to use the php list() function through an example.

Example 1:

<?php
header("content-type:text/html;charset=utf-8"); 
$my_array = array("西门","灭绝","无忌");
list($a,$b,$c) = $my_array;
echo $a.&#39;,&#39;.$b.&#39;和&#39;.$c.&#39;是php中文网的三位老师。&#39;;
?>
Copy after login

Output:

西门,灭绝和无忌是php中文网的三位老师。
Copy after login

Example 2: When the number of variables is greater than the length of the array, list( ) function will report an error.

<?php 
// PHP program to demonstrate the  
// runtime error of list() function  
$array = array(1, 2, 3, 4); 
  
// assign array values to variables  
list($a, $b, $c, $d, $e) = $array;  
  
?>
Copy after login

Output:

How to use php list function

The above is the detailed content of How to use php list function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template