Harry Potter and the deathly H collects array-related functions in PHP

Source: Internet
Author: User
From the beginning of the ASP to PHP, feel that one of the powerful PHP is a rich built-in functions, such as the previous study of the PHP date and time function, read and write the relevant functions of the file, and so on all show that PHP more professional, more user-specific use handy.
At first I am excited about the rich function of PHP function, with more and more nearly abnormal function contact, suddenly think of the ASP built-in function of the sparse, to complete a special function, often need to customize the function, with the increase in application, I actually have a set of commonly used function library. However, now in PHP, these functions have been standardized, standardized and condensed into the built-in functions directly used, the former ASP developers have become the ordinary users of PHP.
But on the other hand, these functions, the existence of a large number of PHP functions, at least illustrate the more professional PHP, but also in our daily PHP program processing should be very quick and easy to use it, so that developers no longer for the basic functions, details function to customize the function, Focus on building more powerful program modules. So, I was more determined to see the PHP function in the end of the belief, but I think in the future development process, the PHP function manual should belong to the book.
Of course, there is no need to discuss the pros and cons of ASP and PHP, and learn to understand the truth.
Words, PHP function too much, to prevent forgetting, so every time after reading a class of functions I do a summary and collection work, convenient to write a log.
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 numeric values.
such as the census of the identity of individuals, such as name, gender, ethnicity, birth, etc. can be used as an array.
Creating arrays in PHP is defined using the array () structure, for example:
$people =array (' name ', ' Sex ', ' nation ', ' brith ');
And how to display the values of each element in the array, we use an index starting at 0, and the index number is in square brackets after the variable name, such as:
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
Echo $people [2];
?>
The output of $people[2] is displayed as nation (the first item in the index is counted from 0).
In addition to supporting numeric index arrays, PHP supports related arrays. The so-called correlation array, is the customizable keyword to replace the non-intuitive digital index, such as:
$peoples =array (' xm ' = ' name ', ' xb ' = ' sex ', ' mz ' = ' nation ', ' cs ' = ' birth ');
echo $peoples [' CS '];
?>
Using a correlation array makes the selection of the output intuitive (no need to pre-compute the index number and then output), and the definition of the "= =" symbol between the defined keywords and values.
Depending on the way the PHP array elements are displayed, you can also create numbers directly, without having to declare and initialize the array () as a variable. Like what
$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 varies dynamically depending on how many elements are added.
2, the display of array elements
In the case of the use of no matter $people[2] or, regardless of $peoples[' CS ' or better, are only the output of the known location of the array element values, how to quickly output all or part of the array elements, using the 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 a loop count.
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
foreach ($people as $xiangmu)
Echo $xiangmu;
?>
The $XIANGMU variable saves the values of each element in the array, which is displayed in turn. Of course, to output the data can be spaced apart, you can output a space after the array element:
echo $xiangmu. " ";
Note: The full period (.) can combine string joins into a new string, see Intimate contact PHP variables, constants learning notes

The above describes the potter and the deathly H collected in PHP related to the array of functions, including Harry Potter and the deathly h aspect of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.