Required for Beginners: PHP array element introduction _ PHP Tutorial

Source: Internet
Author: User
For beginners, see: Introduction to PHP array elements. PHP has been developing for a long time and many users are familiar with PHP. here I will share my personal understanding and discuss with you about PHP array elements. If you have written a story about PHP using a large number of variables over a long period of development, many users are familiar with PHP. here I will share my personal understanding and discuss with you about PHP array elements. If you have written scripts that use a large number of variables (sometimes nearly 100), you will know how uncomfortable it is to trace the content and usage of each variable. Really, I have had this experience. If we can save the variable in another variable, the length of the variable list will be reduced from 100 to less than 10. This is how the array comes from.

An array is a variable that saves variables in the simplest form. This is like a column of houses in a city. The city has many houses, and each house has an address. In the same case, each variable (House) has its own address in an array (city) and we call it an index. Let us assume that you have three names stored in the variables $ sPerson1, $ sPerson2, and $ sPerson3 respectively. Now you can use these three variables in your program, but it is easy to forget which variable is ...... Especially when there are other variables. To put these three variables in an array, you can do the following:

 
 
  1. php
  2. $arrayarrayPeople = array("John", "Susie", "Dave");
  3. ?>

Now, I use $ arrayPeople to replace $ sPerson1, $ sPerson2, and $ sPerson3. Note how I use the array () function in PHP. If these three names are numbers, I will not enclose them in quotation marks. To display these three names, I did this:

 
 
  1. php
  2. $arrayarrayPeople = array("John", "Susie", "Dave");
  3. print $arrayPeople[0];
  4. print $arrayPeople[1];
  5. print $arrayPeople[2];
  6. ?>

Why does it start from scratch? Because the index starts from there. No matter what you put in the array, the index is always automatically accumulated from zero (0. You can manually specify a specific entry for the index. I will talk about this later. Now I will show you how to automatically display the content of an array through a loop:

 
 
  1. php
  2. $arrayarrayPeople = array("John", "Susie", "Dave");
  3. $nArraySize = count($arrayPeople);
  4. for($index=0; $index < $nArraySize; $index++) // max. index is always number of entries - 1
  5. // because index starts at zero
  6. {
  7. print $arrayPeople[$index];
  8. }
  9. ?>

In this case, $ index is the index (address) of the entry, and $ nArraySize is the number of elements in the PHP array. The count () function returns the number of PHP array elements. For the small array I just used, the use of loops does increase the length of the code, but when you start to process hundreds of arrays (they do exist ), you will be happy to use the loop.

Next I will explain how to create your own indexes for arrays. Whenever I use SESSIONS to set administrator permissions for my website, I use arrays to save seesion information. Here is the relevant code

 
 
  1. php
  2. $SESSION= array(); // that creates a blank array
  3. $SESSION["username"] = $sUserName;
  4. $SESSION["password"] = $sPassword;
  5. $SESSION["accesslevel"] = $nLevel;
  6. // etc,etc,etc.
  7. ?>

Have you seen how I use words to represent indexes? In this way, I can know that $ SESSION ["username"] contains the person's name. This is much easier to remember from $ SESSION [0] that it saves the user name. When using arrays, I always use the variable name instead of the index to represent the element. So to save $ nDaysinMay in the array $ arrayDays, I will use $ arrayDays ["nDaysinMay"]. In this way, I can always know what variables are contained in the element.


Bytes. If you have written a large number of variables...

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.