PHP function range () quick method for creating arrays _ PHP Tutorial

Source: Internet
Author: User
PHP function range () to quickly create an array. We are learning the range () function to quickly create a number array from 1 to 9 :? Php $ numbersrange (); use range to directly create 1 ~ 9. an array composed of 9 numbers. we are learningFor example, the range () function can quickly create a number array from 1 to 9:

 
 
  1. Php
  2. $Numbers=Range(); // Use range to directly create 1 ~ 9. an array composed of 9 numbers. it ends with "1" and "9.
  3. Echo $ numbers [1]; // output the second array value created: 2; echo $ numbers [0]; then enter the first value: 0.
  4. ?>

Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, the PHP function range () can also create a character array from a to z:

 
 
  1. Php
  2. $Numbers=Range(A, z );
  3. Foreach ($ numbers as $ mychrs) // traverses the $ numbers array,
    The current unit value of each loop is assigned to $ mychrs.
  4. Echo $ mychrs. ""; // output a B c d e f g h I
    J k l m n o p q r s t u v w x y z
  5. ?>

// Foreach is a simple way to traverse arrays. foreach can only be used for arrays. if you try to use it for another data type or an uninitialized variable, an error will occur, it has two formats:

Foreach (array_expression as $ value) statementforeach (array_expression as $ key => $ value) statement

The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ). In the second format, only the key names of the current unit are assigned to the variable $ key in each loop.

Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different.

The PHP function range () also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7.


For example, the range () function can quickly create a number array from 1 to 9 :? Php $ numbers = range (); // use range to directly create 1 ~ 9. an array composed of 9 numbers, starting with "1...

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.