PHP for beginners PHP array variables using _php tutorial

Source: Internet
Author: User
Tags php for beginners vars
PHPAfter a long period of development, many users are very familiar with PHP, here I publish a personal understanding, and we discuss the discussion. You may be tired of spending a lot of time browsing the external landscape of PHP (learning all about PHP's control structures, operators, and variables). You might even consider quitting the tutorial immediately, and then prefer (about what you would think) to spend your time in front of the TV.

If so, it would be a big mistake. And when I say "big", I mean great. You see, if you give up the tutorial in this chapter because of the charm of Ally McBeal, you will miss the chance to touch one of PHP's coolest variable types. It's a gadget called an array, and I'm not exaggerating when I tell you that once you get used to it, you'll have a little more to say about PHP scripts. But do not take my words as ..., put those still on the side and try it yourself! So far, the variables we're discussing contain only one value, as shown in the code below.

 
  
  
  1. $i

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

 
  
  
  1. Define an array $pizzaToppings = array (' onion ', ' tomato ', ' cheese '

Here, $pizzaToppings is an array variable that contains values such as ' onion ', ' tomato ', ' cheese ', ' anchovies ', ' ham ', and ' pepperoni ' (array variables are especially useful for grouping related values). Print_r () is a special function that allows you to glimpse the value inside a PHP array variable. And for the purpose of displaying the contents of the array, it is more useful for program debugging (to find out why the script failed), but I will use it here so that you can understand what is happening underneath the surface. Make sure your server is running and the browser is open, okay?

The different elements in the array are accessed by index values, and the index value of its first element starts at 0. Therefore, in order to access the element ' onion ', you will use the symbol $pizzatoppings[0], and ' anchovies ' will be $pizzatoppings[3] (essentially, the array variable name is immediately followed by the index value contained in the brackets).

PHP also allows you to use a user-defined "keyword" instead of an index in order to create an array of slightly different types. Each of these keywords is unique and corresponds to a single value in the array.

 
  
  
  1. 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 that contains four key-value pairs. (The-= symbol is used to indicate the association between a keyword and its corresponding value). In order to access the ' banana ' value, you use the $fruits[' yellow ' notation, and the ' grape ' value is accessed through the symbol $fruits[' green '.

Arrays of this type are sometimes referred to as "hash arrays" or "associative arrays." If you've ever used the Perl language, you'll see that it resembles a hash variable in the Perl language.

I hope that the introduction of PHP array variables in this article can bring you some help.


http://www.bkjia.com/PHPjc/445741.html www.bkjia.com true http://www.bkjia.com/PHPjc/445741.html techarticle PHP After a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. Spend a lot of time browsing the external landscape of PHP (learning all ...

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