PHP development is easy to forget some technical details _php skills

Source: Internet
Author: User

When doing development, it is possible to forget some technical details. These details can be very serious consequences, such as the site was injected, the site crashed and so on. Now let's summarize some of the "traps" in PHP that are likely to be encountered.

Trap One: Empty ()

<?php
//The following code will directly cause PHP parsing error
$arr 1 = [1, 2, 3];
$arr 2 = [3, 4];
if (Empty (Array_diff ($arr 1, $arr 2)) {//Parse error
  echo ' empty ';
} else {
  echo ' not empty ';
}

The latest official Website Handbook has a special description of this:

Note:

Before PHP 5.5, empty () only supported variables; anything else would result in a parse error.

In other words, the following code does not take effect: empty (Trim ($name)). Instead, trim ($name) = = false should be used.
I recently encountered this error, is the use of Phalcon development, the server has been reported 503 errors, the beginning will feel inexplicable, by line-by-row exclusion, only occurred due to empty error usage. Of course, since PHP 5.5, empty has supported this type of writing.

Trap two: In_array ()

<?php
//Judge whether the user ID
//$post _dirty_id = ' 1092 ' is present in the array;
$post _dirty_id = ' 1092 order by #1 ';
$safe _arr = [
  987 => ' xiaoming ',
  1092 => ' Tom ',
  1256 => ' Oliver '
];
if (In_array ($post _dirty_id, Array_keys ($safe _arr)) {
  echo ' find Me ';
} else {
  echo ' don't find me '; 
   }
//output result: Find me, this result is obviously wrong

I found this problem because the site was injected into the SQL, fortunately, then when the test was found, did not cause serious consequences.

With regard to the use of the In_array () function, there are other areas worth our attention, the PHP manual, there are a large number of users to provide examples to illustrate the function of the "weird" behavior, such as:

<?php
 $a = [' a ',], True, ' x ' => ' y '];
 Var_dump (In_array, $a)); True, one would expect false
 Var_dump (In_array (' GGG ', $a));//True, one would expect false
 Var_dump (In_array (0, $a)); True
 var_dump (In_array (null, $a));//False

For security reasons, it is recommended that you use the following approach:

<?php
//Judging whether there is a user-submitted ID
//$post _dirty_id = ' 1092 ' in the array;
$post _dirty_id = ' 1092 order by #1 ';
$safe _arr = [
  987 => ' xiaoming ',
  1092 => ' Tom ',
  1256 => ' Oliver '
];
if (Isset ($safe _arr[$post _dirty_id]) {
  echo ' find Me ';
} else {
  echo ' don't find me ';
}
Output result: Do don't find me, this is the correct result

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.