Home > Backend Development > PHP Tutorial > Array-related functions in PHP collected by harry potter and the deathly h

Array-related functions in PHP collected by harry potter and the deathly h

WBOY
Release: 2016-07-29 08:36:51
Original
1118 people have browsed it

From the first introduction to ASP to PHP, I feel that one of the strengths of PHP is the richness of built-in functions. For example, the PHP date and time functions I learned earlier, the related functions for reading and writing files, etc. all show that PHP is more professional and easier for users to use. Handy.
At first I was very excited about the rich functions of PHP functions. As I came into contact with more and more almost abnormal functions, I suddenly thought of the scarcity of ASP built-in functions. To complete a special function, you often need to customize the function. As the number of applications increases, I actually have a set of commonly used function libraries. However, now in PHP, these functions have long been standardized, standardized and condensed into built-in functions for direct use. Former ASP developers have become ordinary users of PHP.
But on second thought, the existence of these functions and a large number of PHP functions at least shows that PHP is more professional; at the same time, it should be very fast and easy to use when processing our daily PHP programs, which makes developers no longer worry about it. Basic functions and detailed functions are removed from custom functions, and the main focus is on building more powerful program modules. Therefore, I have strengthened my belief in taking a look at PHP functions, but I think that in the future development process, the PHP function manual should be a portable book.
Of course, there is no need to discuss the debate about the advantages and disadvantages of ASP and PHP. Learning and understanding can help you understand the truth.
To get back to the subject, there are too many PHP functions to prevent forgetting, so every time I read a type of function, I make a summary and collection work, and write a log for convenience.
1. Definition and initialization of array
What is an array? An array is a programming structure that is a variable that stores a set or series of values.
For example, the identity registration of individuals during the census, such as name, gender, ethnicity, birth, etc., can be used as an array.
Creating an array in PHP is defined using the array() structure, for example:
$people=array('name','sex','nation','brith');
How to display the value of each element in the array? We use an index starting from 0, and the index number is in square brackets after the variable name, for example:
$people=array('name','sex','nation','birth') ;
echo $people[2];
?>
The output $people[2] shows nation (the first item of the index counts from 0).
PHP not only supports numerical index arrays, but also supports related arrays. The so-called related array means that you can use custom keywords to replace unintuitive numerical indexes, such as:
$peoples=array('xm'=>'name','xb'=>'sex' ,'mz'=>'nation','cs'=>'birth');
echo $peoples['cs'];
?>
Using related arrays makes the selection of output intuitive (no need to pre- Calculate the index number and then output), use the "=>" symbol between the defined keyword and value.
According to the two display methods of PHP array elements, numbers can be automatically created directly without array() declaration and initialization like variables. For example
$people[0]='name';
$people[1]='sex';
$people[2]='nation';
$people[3]='brith';
or
$peoples ['xm']='name';
$peoples['xb']='sex';
$peoples['mz']='nation';
$peoples['cs']='birth';
The size of the array changes dynamically based on how many elements are added.
2. Display of array elements
No matter $people[2] or $peoples['cs'] used above, it only outputs the array element value of the known clear position. How to quickly output all or For some array elements, using a loop statement is undoubtedly the fastest way.
$people=array('name','sex','nation','birth');
for ($i=0;$i<4;$i++)
echo "$people[ $i] ";
?>
In addition to using a for loop that knows the number of loops, you can also use a foreach statement that does not require the number of loops.
$people=array('name','sex','nation','birth');
foreach($people as $xiangmu)
echo $xiangmu;
?>
$xiangmu variable The values ​​of each element in the array will be saved and displayed in sequence. Of course, in order to distinguish the output data, a space can be output after the array elements:
echo $xiangmu." ";
Note: English periods (.) can merge string connections into new strings, see Intimate Contact PHP Variables and constants study notes

The above introduces the array-related functions in PHP collected by harry potter and the deathly h, including the content of harry potter and the deathly h. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template