PHP quick way to create arrays (function range () application)

Source: Internet
Author: User
    1. $numbers =range (1,9); Create an array of 1~9 with a range of 9 numbers directly, ending with "1" Starting with "9".
    2. echo $numbers [1]; The output creates a second array value: 2; echo $numbers [0]; Enter the first value: 0.
    3. ?>
Copy Code

Of course, using range (9,1) creates an array of numbers from 9 to 1. At the same time, the PHP function range () can also create an array of characters from A to Z:

    1. $numbers =range (a,z);
    2. foreach ($numbers as $mychrs)//traversal $numbers array,
    3. Each time the current cell value of the loop is assigned to $mychrs
    4. echo $mychrs. " "; Output a b c d e F g h i
    5. J k l m n o p q R S t u v w x y Z
    6. ?>
Copy Code

foreach is an easy way to iterate through an array, and foreach can only be used with arrays, which can produce an error when trying to use it for other data types or an uninitialized variable, in two formats: foreach (array_expression as $value) Statementforeach (array_expression as $key = $value) statement

The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken). The second format does the same thing, except that the key name of the current cell is assigned to the variable in each loop $key

Note the case when using a character array, such as range (A,Z) and range (A,Z) are not the same.

The PHP function range () also has a third parameter that sets the step size, such as the array elements created by range (1,9,3): 1, 4, 7.

About the use of the PHP Array function range () is finished, I wish you all the progress of learning.

  • Related Article

    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.