Home  >  Article  >  Backend Development  >  PHP Advanced: Use Arrays to Complete Powerful Functions

PHP Advanced: Use Arrays to Complete Powerful Functions

高洛峰
高洛峰Original
2016-12-02 10:29:22784browse

The use of arrays in PHP is at least simpler than in C. The concept of arrays is rarely used in general PHP programs. Judgments, loops and cookies are mostly used, but it is very important to master the skills of using arrays. , because it can make the functions of the program more automated, allowing customers to choose between a program with powerful functions but poor operating performance and a program with small and exquisite functions but very convenient operation. I think customers will choose the latter.

Before introducing the use of arrays, I I have to state here that I am only introducing the idea of ​​using arrays, that is, under what circumstances arrays can be used to simplify the completion of tasks. The examples given are only to provide an idea and will not cover everything.

When to use arrays, generally Consider using arrays when encountering large-scale variable transmission and processing.

An example of how I used arrays to complete tasks:

Once I wrote a background management program for a customer. After the program was completed, the customer was very satisfied , but after a while the customer came to me and said to me: "I found that there was a lot of junk information in it, and I didn't want to keep it. I was in trouble when deleting it. I had to click 'Delete' every time." To delete every message, can you help me design a function that can delete all junk at once...";Of course I accepted this task, because the customer is God.

When I started to add this function into the management program, I considered using an array to complete it, so I wrote a flow chart:

Find the unique identifier id of the spam message --> Array --> Delete the database record corresponding to the id in the array

Okay, this is the flow chart , you can start designing the program. In order to use the array to successfully transfer variables, I used the checkbox of the submission table. The specific writing method:

echo "";

In this way, I add the spam id to the space corresponding to the array subscript, so that I can perform actual operations on the database. Let's take a look at how the task is completed when the array is passed to the delete function block. ;

//First find the ids of all records in the database:
.....
for ($i=0;$i<$num;$i++) {
$id=mysql_result($result,$i, "id");
//Judge array
if (!empty($delete[$id])) {
//Execute delete command
mysql_query("delete from table where id=$id");
}
}
...

An automated deletion function has been completed. Arrays can also be used to complete many different tasks such as: randomly updating pictures, comparisons, etc...

Using arrays can complete some tasks that may seem difficult to complete. mission

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn