The list function in PHP

Source: Internet
Author: User

Today in the development of a method to see the list language structure, just beginning to think it is a function in PHP!


List () is used to assign values to a set of variables in a single operation.

Note: List () is used only for arrays of numeric indexes and assumes that the numeric index starts at 0.

Description

List () assigns a value to a set of variables using the elements in the array.

Note that, like array (), list () is actually a language structure, not a function.

Such as:

<?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;
?>

The results are as follows and can be output normally

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1043589/201611/ 1043589-20161118144822779-1169439759.png "/>

When an array is not a numeric array but an associative array, such as:

<?php
$my _array = Array (' a ' = = ' Dog ', ' b ' = ' Cat ', ' c ' = ' Horse ');
List ($a, $b, $c) = $my _array;
Echo ' I have several animals, a '. $a. ', a '. $b. ' and a '. $c;
?>

The result will be an error.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1043589/201611/ 1043589-20161118145321185-1621094066.png "/>

When using an indexed array is as follows:

<?php
$my _array = Array (0=> ' Dog ',1=> ' Cat ',2=> ' Horse ');
List ($a, $b, $c) = $my _array;
Echo ' I have several animals, a '. $a. ', a '. $b. ' and a '. $c;
?>

This is a normal output when the code is changed to

<?php
$my _array = Array (1=> ' Dog ',2=> ' Cat ',3=> ' Horse ');
List ($a, $b, $c) = $my _array;
Echo ' I have several animals, a '. $a. ', a '. $b. ' and a '. $c;
?>

The output is as follows

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1043589/201611/ 1043589-20161118150139138-2090392627.png "/>


The list function in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.