Use of PHP array variables for beginners of PHP

Source: Internet
Author: User

PHPAfter a long period of development, many users are familiar with PHP. Here I will share my personal understanding and discuss it with you. You may be bored with spending a lot of time browsing the external scenery of PHP (learning all about the control structures, operators, and variables of PHP. You may even want to quit the tutorial immediately and prefer to spend the time in front of the TV (as you may think.

In this case, it would be a big mistake. When I say "big", I mean huge. You see, if you give up learning this tutorial due to the charm of Ally McBeal, you will miss a chance to get in touch with one of the coolest variable types in PHP. It is a little bit called "array", and I am not exaggerating to say that when I tell you that once you adapt to its usage, you will look at the PHP script in a different way. However, do not treat my words ......, Try those on the other side! So far, the variables we have discussed only contain one value, as shown in the following code.

 
 
  1. <?php $i = 5; ?> 

However, array variables are completely different. An array is a complex variable type that allows you to store multiple values in a single variable (it can be easily done when you need to store and describe the relevant information ). We can treat the PHP array variable as a "Container" variable that can hold one or more values. For example:

 
 
  1. <?php // define an array $pizzaToppings = array('onion', 'tomato', 'cheese' 

Here, $ pizzaToppings is an array variable, it contains 'Onion ', 'tomato', 'cheese', 'anchovies', 'ham', and 'pepperon' values (array variables are especially useful for grouping related values ). Print_r () is a special function that allows you to see values in PHP array variables. Compared with the purpose of displaying the array content, it is more useful for program debugging (identify the cause of script failure, but I will use it here so that you can understand what is happening on the surface. Are you sure you want to run your server and open your browser?

Different elements in the array are accessed through index values, and the index value of the first element starts from 0. Therefore, to access the element 'Onion ', you will use the symbol $ pizzaToppings [0], and 'anchovies' will be $ pizzaToppings [3] (essentially, the name of the array variable is followed by the index value included in the brackets ).

PHP also allows you to replace indexes with user-defined "keywords" to create an array of slightly different types. Each keyword is unique and corresponds to a single value in the array.

 
 
  1. <?php // define an array $fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' =>   
  2. 'plum', 'green' => 'grape'); print_r($fruits); ?> 

In this instance, $ fruits is a PHP array variable containing four key value pairs. (=> A symbol is used to indicate the association between a keyword and its corresponding value ). To access the 'bana' value, you need to use the $ fruits ['yellow'] symbol, while the 'grape' value is accessed by the symbol $ fruits ['green.

This type of array is sometimes called a "hash array" or "Join array ". If you have used the Perl language, you will see it is similar to the hash variable in the Perl language.

I hope this article will help you with the introduction of PHP array 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.