Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the elements of the PHP array

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what are the elements of the PHP array, I hope you will gain something after reading this article, let's discuss it together!

PHP after a long period of development, many users are very familiar with PHP, here I would like to publish a personal understanding, and discuss with you about PHP array elements. If you've ever written scripts that use a large number of variables (sometimes nearly 100), you know how hard it is to track the content and purpose of each variable. Really, I've had this experience before. 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 where the array comes from.

An array, in its simplest form, is a variable that holds variables. It's a lot like a row of houses in a city. The city has many houses, and each house has an address. Similarly, each variable (house) has its own address in an array (city), which we call an index. Let's assume that you have three names stored in variables called $sPerson1, $sPerson2, and $sPerson3. Now you can use these three variables in your program, but it's easy to forget which variable is which. Especially when there are other variables. To put these three variables in an array, you can do something like this:

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

Why start from scratch? Because that's where the index starts. No matter what you put in the array, the index always accumulates automatically from zero (0). You can manually specify a specific entry for the index, which I'll talk about in a moment. Now I'll show you how to automatically display the contents of an array through a loop:

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 elements in the PHP array. For decimal arrays like the one I just used, using loops does increase the length of the code, but when you start working with hundreds of arrays of elements (they do exist), you'll be happy to use loops.

Next I'll talk about how to create your own index for an array. Whenever I use SESSIONS to set administrator permissions for my site, I use arrays to save seesion information. Here is the relevant code

See how I use words to represent indexes? This way I can know that $SESSION ["username"] contains a person's name. This is much easier than remembering from $SESSION [0] that it saves the user name. When I use an array, I always use the name of the variable instead of the index to represent the element. So to save $nDaysinMay in the array $arrayDays, I'll use $arrayDays ["nDaysinMay"]. So I can always know what variables are contained in the element.

After reading this article, I believe you have a certain understanding of "what are the elements of the PHP array". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report