PHP development in the most common 10 mistakes

Source: Internet
Author: User
Keywords nbsp; value array below 2345
Tags aliyun array change code copy data developed development

PHP is a very popular open source server-side scripting language, and most of the sites you see on the World Wide Web are developed using PHP. This experience will introduce you to the 10 most common PHP development problems, hoping to be helpful to friends.

Error 1: Leave a dangling pointer after the foreach loop

In a foreach loop, it's a good idea to use references if we need to change the elements of an iteration or to improve efficiency at http://www.aliyun.com/zixun/aggregation/7208.html "

12345 $ arr = array (1,2,3,4); foreach ($ arras & $ value) {$ value = $ value * 2;} // $ arr is now array (2, 4, 6, 8)

There is a problem here, many people will be confused. $ Value is not destroyed, $ value is actually a reference to the last element in the array, so in the subsequent use of $ value, if you do not know this, it will lead to some baffling mistakes :) See below This code:

$ Arrayas & $ value} {} / / echoimplode (',', $ array), by the reference echoimplode (',', $ array), "\ n"; foreach foreach ($ arrayas $ value) {} // echoimplode (',', $ array), '\ n'; by value (ie, copy)

The above code results are as follows:

1231,2,31,2,31,2,2

You guessed it? Why is this result?

Let's analyze it. After the first iteration, $ value is the reference to the last element in the array. The second cycle begins:

Step 1: Copy $ arr [0] to $ value (note that $ value is a reference to $ arr [2]), then the array becomes [1,2,1] Step 2: Copy $ arr [1 ] To $ value, then the array becomes [1,2,2] The third step: copy $ arr [2] to $ value, then the array becomes [1,2,2]

In summary, the end result is 1,2,2

The best way to avoid this error is to destroy the variable with the unset function immediately after the loop:

12345 $ arr = array (1,2,3,4); foreach ($ arras & $ value) {$ value = $ value * 2;} unset ($ value); // $ value no longer references $ arr [2] Error 2: A misunderstanding of the isset () function's behavior

For the isset () function, false is returned if the variable does not exist, or false if the variable is null. This behavior is easy to get confused. . . Look at the following code:

1234 $ data = fetchRecordFromStorage ($ storage, $ identifier); if (! Isset ($ data ['keyShouldBeSet']) {// do something here if 'keyShouldBeSet' is not set}

The intention of people writing this code might be to execute the corresponding logic if $ data ['keyShouldBeSet'] is not set. The problem is that even if $ data ['keyShouldBeSet'] is set, but the value is set to null, or the corresponding logic is executed, this is not what the code meant.

Here's another example:

123456789if ($ _ POST ['active']) {$ postData = extractSomething ($ _ POST);} // ... if (! Isset ($ postData)) {echo'post not active ';}

The code above assumes that $ _POST ['active'] is true, so $ postData should be set, so isset ($ postData) will return true. Conversely, the code above assumes that the only way isis ($ postData) returns false is that $ _POST ['active'] also returns false.

Really? of course not!

Even if $ _POST ['active'] returns true, $ postData may be set to null, and isset ($ postData) will return false. This does not meet the original intention of the code.

If the above code was only intended to detect $ _POST ['active'] is true, the following will be better:

123456789if ($ _ POST ['active']) {$ postData = extractSomething ($ _ POST);} // ... if (! $ _ POST ['active']) {echo'post not active ';

Determine whether a variable is really set (distinction between the set and null), array_key_exists () function may be better. Reconstruction The first example above, is as follows:

1234 $ data = fetchRecordFromStorage ($ storage, $ identifier); if (! Array_key_exists ('keyShouldBeSet', $ data)) {// do this if 'keyShouldBeSet' is not set}

In addition, in conjunction with the get_defined_vars () function, we can more reliably detect whether variables are set within the current scope:

123if (array_key_exists ('varShouldBeSet', get_defined_vars ())) {// variable $ varShouldBeSet exists in current scope} Error 3: Obfuscate return value and return reference

Consider the following code:

$ Config = newConfig (); $ config-> getValues ​​() ['test'] = 'test'; echo $ config- > getValues ​​() ['test'];

Running the above code will output the following:

PHP Notice: Undefined index: test in /path/to/my/script.php on line 21

Where is the problem? The problem is that the code above obfuscates the return value and returns the reference. In PHP, unless you show the specified return reference, the return value for array PHP is the copy of the array. Therefore, the above code on the return array assignment, the actual copy array assignment, non-original array assignment.

Related Article

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.