Home  >  Article  >  Backend Development  >  Example of usage of list method in PHP

Example of usage of list method in PHP

黄舟
黄舟Original
2017-02-24 09:34:401253browse

The example in this article describes the usage of the list method in PHP. Share it with everyone for your reference, the details are as follows:

<?php
function small_numbers()
{
  return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
var_dump($zero);
var_dump($one);
var_dump($two);
?>

Output:

int(0)
int(1)
int(2)

Change it

<?php
function small_numbers()
{
  return array (0, 1);
}
list ($zero, $one, $two) = small_numbers();
var_dump($zero);
var_dump($one);
var_dump($two);
?>

Output:

Notice: Undefined offset: 2 in D:\xampp\htdocs\t\test.php on line 6
int(0)
int(1)
NULL

Change it again

<?php
function small_numbers()
{
  return array (0, 1 ,2 ,3);
}
list ($zero, $one, $two) = small_numbers();
var_dump($zero);
var_dump($one);
var_dump($two);
?>

Output:

int(0)
int(1)
int(2)

Change it again and learn by drawing inferences

<?php
function small_numbers()
{
  return array (0, 1 , array(&#39;2&#39;));
}
list ($zero, $one, $two) = small_numbers();
var_dump($zero);
var_dump($one);
var_dump($two);
?>

Output:

int(0)
int(1)
array(1) {
[0]=>
string(1) "2"
}

The above is the content of the usage examples of list method in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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