PHP array definition, initialization and the display of the elements of the Code _php base

Source: Internet
Author: User
Tags array definition arrays

From the beginning of ASP to PHP, I feel that one of the powerful PHP is the rich built-in functions, such as the previous study of PHP date and time function, read and write the relevant functions of the file are all show that PHP more professional, more user-friendly use.

At first I was excited about the richness of PHP functions, with more and more nearly perverted function contact, suddenly associated with the lack of ASP built-in functions, to complete a special function, often need to customize functions, with the application in the increase, they actually have a set of commonly used function library. However, now in PHP, these functions have been standardized, standardized and condensed into a built-in function for direct use, the former ASP developers have become a common user of PHP.
But on second thought, these functions, the existence of these large PHP functions, at least illustrate the more professional PHP, and in our day-to-day PHP program processing should be very fast and easy to use it, so that developers no longer for some basic functions, details of the function to go to custom, Focus your efforts on building more powerful program modules. So, I was more determined to see the PHP function in the end of the faith, but I think in the future development process, PHP function manual should belong to the book.

Of course, there's no need to talk about the pros and cons of ASP and PHP, to learn and learn to understand the truth.

Words, PHP functions 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 diary.

1, the definition and initialization of the 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, when the census of personal identity registration, such as name, gender, ethnicity, birth, etc. can be used as an array.
The creation of an array in PHP is defined using the array () structure, such as:

$people =array (' name ', ' Sex ', ' nation ', ' brith ');

And how to display the values of each element in an array, we use an index that starts with 0, and the index number is in the square brackets after the variable name, for example:

<?php
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
Echo $people [2];
? >

The $people[2 of the output] is the display of nation (the first item of the index counts from 0).

In addition to supporting numeric indexed arrays, PHP supports related arrays. An associated array is a customizable keyword that replaces an intuitive digital index, such as:

<?php
$peoples =array (' xm ' => ' name ', ' xb ' => ' sex ', ' mz ' => ' nation ', ' cs ' => ' birth ');
echo $peoples [' CS '];
? >

Using an associated array makes the selection of the output intuitive (without the need to calculate the index number and then output), with the "=>" symbol defined between the defined keyword and the value.

Depending on the two ways in which the elements of the PHP array are displayed, you can also automatically create numbers directly, without the need for array () Declaration and initialization, as with variables. 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 according to how much of the added element is dynamic.

2, the display of array elements

Regardless of the use of the $people[2], regardless of the $peoples[' CS ', it is only the output of the known clear 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.

<?php
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
For ($i =0 $i <4; $i + +)
  echo "$people [$i]";
? >

In addition to using a For loop that understands the number of loops, you can use a foreach statement that does not need to be required for the number of loops.

<?php
$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, for the output data to be spaced apart, the space can be output after the array element:
echo $xiangmu. " ";
Note: The English period (.) can combine string concatenation into a new string

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.