Function excerpt related to arrays in PHP

Source: Internet
Author: User
Tags array definition
From the beginning of ASP to PHP, I feel that one of the strong PHP is the rich built-in functions, such as the PHP date and time functions I learned earlier, all the coherent functions used to read and write files indicate that PHP is more professional and user-friendly.

 

From the beginning of ASP to PHP, I feel that one of the strong PHP is the rich built-in functions, such as the PHP date and time functions I learned earlier, all the coherent functions used to read and write files indicate that PHP is more professional and user-friendly.

At the very beginning, I was very happy with the rich functions of PHP functions. as more and more functions become available, I suddenly think of ASP built-in functions as sparse, to accomplish a special function, you often need to define a function. as the number of applications increases, you actually have a set of common function libraries. However, in PHP, these functions have already been scaled and standardized and concentrated into built-in functions for direct application. ASP developers once became common PHP users.

But in another thought, the existence of these functions and a large number of PHP functions should at least clarify the more professional PHP functions. at the same time, it should be quick and easy to use in our daily PHP program processing, this allows developers to no longer customize functions for basic functions and detailed functions, and focus their important efforts on building stronger program modules. Therefore, I have strengthened my confidence in PHP functions, but I think the PHP function manual should be a portable book in the future development process.

Of course, there is no need to discuss the issues of ASP and PHP advantages and disadvantages. learning and understanding can help you understand the truth.

As the saying goes, there are too many PHP functions to prevent forgetting, so I will make a summary and collection every time after reading a class of functions, so I will write a log for convenience.

1. array definition and initialization

What is an array? An array is a programming structure. it is a variable that stores a group or a series of values.

For example, personal identity registration during census, such as name, gender, ethnicity, and birth, can be used as an array.

PHP creates an array and uses the array () structure to define it. for example:


$ People = array ('name', '***', 'nation', 'brith ');

How to display the values of each element in the array is the index starting from 0. The index number is in square brackets after the variable name. for example:


$ People = array ('name', '***', 'nation', 'birth ');
Echo $ people [2];
?>

The output $ people [2] indicates that nation is displayed (the first index item is counted from 0 ).

In addition to supporting numeric index arrays, PHP also supports coherent arrays. The so-called coherent array is the ability to customize key words to replace inintuitive digital indexes, such:


$ Les = array ('xm '=> 'name', 'xb' => '***', 'mz' => 'nation ', 'CS '=> 'birth ');
Echo $ doneles ['CS '];
?>

Apply a coherent array to make the output selection intuitive (you do not need to calculate the index number and then output it), and apply the "=>" symbol between the key words and values of the definition.

Based on the two display methods of PHP array elements, you can also create numbers directly without the need for array () declaration and initialization like variables. For example


$ People [0] = 'name ';
$ People [1] = '***';
$ People [2] = 'Nation ';
$ People [3] = 'brith ';

Or


$ Les ['xm '] = 'name ';
$ Cmdles ['xb '] = '***';
$ Doneles ['mz'] = 'Nation ';
$ Les ['CS '] = 'birth ';

The size of the array is dynamically changed based on the number of added elements.

2. display of array elements

In the above application, no matter $ people [2] or $ minutes les ['CS '], only the known array element values are output, how to quickly output all or part of the array elements is undoubtedly the fastest way to apply loop statements.


$ People = array ('name', '***', 'nation', 'birth ');
For ($ I = 0; $ I <4; $ I)
Echo '$ people [$ I]';
?>

In addition to the for loop that the application understands the number of loops, you can also apply the foreach statement that does not need to be requested for the number of loops.

$ People = array ('name', '***', 'nation', 'birth ');
Foreach ($ people as $ xiangmu)
Echo $ xiangmu;
?>

The $ xiangmu variable keeps the element values in the array and displays them in sequence. Of course, in order to differentiate the output data at intervals, spaces can be output after the array elements:
Echo $ xiangmu .'';
Note: The English periods (.) can be used to merge string connections into new strings. For more information, see closely accessing PHP variables and constant learning notes.

 

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.