PHP array application basics (4)

Source: Internet
Author: User
Abstract: There are two common methods to traverse arrays. one is for loop statement traversal and the other is foreach statement traversal... 1. Traverse arrays.

There are two common methods to traverse arrays. one is for loop statement traversal and the other is foreach statement traversal:

For loop statement traversal: for loop statements can only traverse enumerated arrays whose subscript is continuous.

Foreach statement traversal: foreach is a statement designed specifically to traverse arrays. foreach does not need to consider whether the subscript of an array is a continuous number. as long as it is a PHP array, foreach can be used for traversal. Code demonstration:

$ Meiju_arr = array ("pig", "www.wozhutou.com", "E-Commerce", "male", "Web engineer ");
// For loop statement traversal
For ($ I = 0; $ I <count ($ meiju_arr); $ I ++ ){
Echo "number". $ I. "element value:". $ meiju_arr [$ I]; // output the value of the array element indexed to I
} Here, the count () function is used to calculate the number of array elements in the array.
// The foreach statement traverses the array as a value
Foreach ($ meiju_arr as $ value ){
Echo $ value;
}

// The foreach statement traverses the array as a key-value pair.
Foreach ($ meiju_arr as $ key => $ value ){
Value of the element whose echo "index is". $ key. ":". $ value;
}
2. system pre-defined array

These functions are very powerful. Must be used

$ _ SERVER: SERVER variable array, an array created by the Web SERVER. its content includes header information, path, script location, and other information.

$ _ ENV: environment variable array, which records information related to the system running PHP, such as the system name and system path.

$ _ GET: an array composed of variables passed by the HTTP get method

$ _ POST: an array composed of variables passed by the HTTP post method

$ _ REQUEST: an array of all input variables, including $ _ GET, $ _ POST, and $ _ COOKIE.

$ _ FILES: an array consisting of uploaded File items passed by the HTTP post method

$ _ COOKIE: an array composed of variables passed by HTTP Cookies.

$ _ SESSION: an array containing SESSION variables in the current script

This is the end of today.

The above is the basic content of PHP array application (4). For more information, please follow the PHP Chinese network (www.php1.cn )!

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.