Home  >  Article  >  Backend Development  >  What should you pay attention to when using the list function in PHP?

What should you pay attention to when using the list function in PHP?

青灯夜游
青灯夜游Original
2019-05-20 11:10:374224browse

What should you pay attention to when using the list function in PHP?

In PHP, the list() function is used to assign values ​​to a set of variables in one operation.

Example:

<?php
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>

Output:

I have several animals, a Dog, a Cat and a Horse.

When using the list() function, you need to pay attention:

● list() actually It is a language structure, not a function.

● The list() function is only used in numerically indexed arrays

We can look at the my_array array in the above example:

<?php
$my_array = array("Dog","Cat","Horse");
var_dump($my_array);
?>

Output:

array (size=3)
  0 => string &#39;Dog&#39; (length=3)
  1 => string &#39;Cat&#39; (length=3)
  2 => string &#39;Horse&#39; (length=5)

It can be seen that the index of the my_array array is a numeric index.

● Assuming that the numerical index starts from 0, that is, assigning values ​​to variables starting from index 0

If the corresponding numerical index does not exist, the variable in the corresponding bit will have a null value.

The above is the detailed content of What should you pay attention to when using the list function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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