Home >Backend Development >PHP Tutorial >Collection of array-related functions in PHP_PHP tutorial
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 it. Functions, with the increase in applications, I actually have a set of commonly used function libraries. However, now in PHP, these functions have long been standardized, normalized 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 Customize functions for some basic functions and detailed functions, and focus your main energy 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 be honest, there are too many functions in PHP to prevent forgetting, so every time I read a type of function, I will summarize and collect it, and write a log for convenience.
1. Definition and initialization of arrays
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 array For the value of each element, we use the index starting from 0. 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 in the index counts from 0 ).
In addition to supporting numerical index arrays, PHP also supports related arrays. The so-called related array means that you can customize keywords to replace unintuitive numerical indexes, such as:
$peoples=array('xm'=>'name','xb'=> ;'sex','mz'=>'nation','cs'=>'birth');
echo $peoples['cs'];
?>
Use related array To make the selection of output intuitive (no need to pre-calculate the index number and then output it), the defined keywords and values are defined using the "=>" symbol.
According to the two display methods of PHP array elements, numbers can be automatically created directly without the need for 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 this array changes dynamically according to the number of added elements.
2. Display of array elements
Regardless of whether $people[2] or $peoples['cs'] are used above, they only output the array element value of the known and clear position. How? To quickly output all or part of the 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 will save the values of each element in the array and display them in sequence. Of course, in order to distinguish the output data from each other, a space can be output after the array elements:
echo $xiangmu." ";
Note: English periods (.) can concatenate strings into new strings. See the study notes on variables and constants in close contact with PHP