Analysis of the 10 most common errors in PHP programming, a brief analysis of PHP programming 10_php tutorial

Source: Internet
Author: User
Tags coding standards form post ord php and mysql php programming strcmp zend framework

Analysis of the 10 most common mistakes in PHP programming 10

Currently learning PHP a lot of friends, in peacetime daily program development project always encounter a variety of problems, this experience will introduce PHP development in the 10 most common problems, I hope to be able to help friends.

Error 1:foreach left hanging pointer after loop

In a foreach loop, if we need to change the elements of an iteration or to improve efficiency, applying a reference is a good idea:

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

There's a problem here, and a lot of people get confused. At the end of the loop, value is not destroyed, and 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, there will be some inexplicable error:) look at the following code:

$array = [1, 2, 3]; echo implode (', ', $array), ' \ n ';  foreach ($array as & $value) {}  /by reference echo implode (', ', $array), "\ n";  foreach ($array as $value) {}   //by value (i.e., copy) echo implode (', ', $array), "\ n";

The result of the above code is as follows:


Have you guessed it right? Why is this result?

Let's analyze that. After the first loop, $value is a reference to the last element in the array. The second loop begins:

First step: Copy arr[0] to value (note at this point that value is a reference to arr[2], when the array becomes [1,2,1]
Step two: Copy arr[1] to value, when the array becomes [1,2,2]
Step three: Copy arr[2] to value, when the array becomes [1,2,2]
In conclusion, the end result is 1,2,2

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

$arr = Array (1, 2, 3, 4); foreach ($arr as & $value) {   $value = $value * 2;} unset ($value);  $value no longer references $arr [3]

Error 2: Incorrect understanding of the behavior of the isset () function

For the Isset () function, False is returned when the variable does not exist, and false when the value of the variable is null. It's easy to confuse people with this behavior ... Look at the following code:

$data = Fetchrecordfromstorage ($storage, $identifier); if (!isset ($data [' Keyshouldbeset ']) {   //do something here if ' keyshouldbeset ' are not set}

The person who wrote this code might have intended to execute the corresponding logic if data[′keyshouldbeset′] was not set. The problem is that even if data[' Keyshouldbeset ' is set, but the value is set to NULL, the corresponding logic is executed, which does not conform to the code's original intent.

Here is another example:

if ($_post[' active ']) {   $postData = extractsomething ($_post);}  // ...  if (!isset ($postData)) {   echo ' post not active ';}

The above code assumes that post[′active′] is true, then postdata should be set, so Isset (PostData) returns True. Conversely, the above code assumes that the only way to return false for Isset (PostData) is to return false $_post[' active '.

Is that true? Of course not!

Even if post[′active′] returns True,postdata may also be set to NULL, Isset ($postData) will return false. This does not conform to the original meaning of the code.

If the above code is intended only to detect whether $_post[' active ' is true, the following implementation would be better:

if ($_post[' active ']) {   $postData = extractsomething ($_post);}  // ...  if ($_post[' active ']) {   echo ' POST not active ';}

The array_key_exists () function may be better to determine whether a variable is actually set (the distinction is not set and the value is set to null). Refactoring the first example above, as follows:

$data = Fetchrecordfromstorage ($storage, $identifier); if (! array_key_exists (' Keyshouldbeset ', $data)) {   //do this if ' keyshouldbeset ' isn ' t set}

In addition, with the Get_defined_vars () function, we can more reliably detect whether a variable is set within the current scope:

if (array_key_exists (' Varshouldbeset ', Get_defined_vars ())) {   //variable $varShouldBeSet exists in current scope}

Error 3: Confusing return value and returning reference

Consider the following code:

Class Config {   private $values = [];    Public Function GetValues () {     return $this->values;   }}  $config = new config ();  $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

What is the problem? The problem is that the code above confuses the return value and returns the reference. In PHP, unless you display the specified return reference, otherwise for the array PHP is the value returned, which is the copy of the array. So the above code assigns a value to the returned array, which is actually the assignment of the copy array, not the original array assignment.

GetValues () Returns a copy of the $values array, so this adds a ' test ' element//to a copy of the $values array, but n OT to the $values array itself. $config->getvalues () [' test '] = ' test ';  GetValues () again returns another COPY of the $values array, and this COPY doesn ' t//contain a ' test ' element (which I s why we get the "undefined index" message). echo $config->getvalues () [' Test '];

The following is a possible workaround for outputting a copy of an array, rather than the original array:

$vals = $config->getvalues (); $vals [' test '] = ' test '; echo $vals [' Test '];

If you want to change the original array, that is, to reverse the array reference, what should be done? The solution is to display the specified return reference:

Class Config {   private $values = [];    Return a REFERENCE to the actual $values array public   function &getvalues () {     return $this->values;   }}  $config = new config ();  $config->getvalues () [' test '] = ' test '; echo $config->getvalues () [' Test '];

After the transformation, the above code will output test as you would expect.

Let's look at an example that will make you more confused:

Class Config {   private $values;    Using arrayobject rather than array public   function __construct () {     $this->values = new Arrayobject ();   } public    function GetValues () {     return $this->values;   }}  $config = new config ();  $config->getvalues () [' test '] = ' test '; echo $config->getvalues () [' Test '];

If you want to output the "Undefined index" error as above, you are wrong. The code will output "test" normally. The reason for this is that PHP returns the object by reference by default, rather than by value.

In summary, when we use the function return value, we need to figure out whether the value returns or the reference is returned. In PHP, for objects, the default is a reference return, and the array and built-in base types are returned by value by default. This is to be distinguished from other languages (many languages are reference to arrays).

Like other languages, such as Java or C #, using Getter or setter to access or set class properties is a better solution, of course, PHP is not supported by default and requires its own implementation:

Class Config {   private $values = [];    Public Function SetValue ($key, $value) {     $this->values[$key] = $value;   }    Public Function GetValue ($key) {     return $this->values[$key];   }}  $config = new config ();  $config->setvalue (' TestKey ', ' testvalue '); echo $config->getvalue (' TestKey ');  Echos ' TestValue '

The above code gives the caller the ability to access or set any value in the array without giving access to the array public. How it feels:)

Error 4: Executing SQL query in loop

It is not uncommon to find code similar to the following in PHP programming:

$models = [];  foreach ($inputValues as $inputValue) {   $models [] = $valueRepository->findbyvalue ($inputValue);}

Of course, there is nothing wrong with the above code. The problem is that we $valuerepository->findbyvalue () may execute SQL queries every time during the iteration:

$result = $connection->query ("Select ' x ', ' y ' from ' values ' WHERE ' value ' =". $inputValue);

If you iterate 10,000 times, you execute 10,000 SQL queries separately. If such a script is called in a multithreaded program, it is likely that your system will hang up ...

In the process of writing your code, you should be aware of when you should execute the SQL query and take all the data out of the SQL query as soon as possible.

There is a business scenario where you are likely to make the above mistake. Suppose a form submits a series of values (assuming IDs), and then in order to fetch the data for all IDs, the code iterates through IDs and executes the SQL query for each ID, as shown in the following code:

$data = []; foreach ($ids as $id) {   $result = $connection->query ("Select ' x ', ' y ' from ' values ' WHERE ' id ' =". $ID);   $data [] = $result->fetch_row (); }

But the same goal can be done more efficiently in a SQL, with the following code:

$data = []; if (count ($ids)) {   $result = $connection->query ("Select ' x ', ' y ' from ' values ' WHERE ' ID ' in ("). Implode (', ', $ids));   while ($row = $result->fetch_row ()) {     $data [] = $row;   }}

Error 5: Memory usage inefficient and illusion

Once a SQL query gets multiple records, it is certainly more efficient to get one record per query, but if you are using MySQL extensions in PHP, getting multiple records at one time is likely to cause a memory overflow.

We can write code to experiment (test environment: 512MB RAM, MySQL, PHP-CLI):

Connect to MySQL $connection = new mysqli (' localhost ', ' username ', ' password ', ' database ');  CREATE TABLE of columns $query = ' CREATE table ' Test ' (' ID ' INT not NULL PRIMARY KEY auto_increment '; for ($col = 0; $col < 400; $col + +) {   $query. = ", ' Col$col ' CHAR (Ten) Not NULL";} $query. = '); $connection->query ($query);  Write 2 million rows for ($row = 0; $row < 2000000; $row + +) {   $query = "INSERT into ' Test ' VALUES ($row";   for ($col = 0; $col < $col + +) {     $query. = ', '. Mt_rand (1000000000, 9999999999);   }   $query. = ') ';   $connection->query ($query); }

Now let's look at resource consumption:

Connect to MySQL $connection = new mysqli (' localhost ', ' username ', ' password ', ' database '); echo "Before:". Memory_get_peak_usage (). "\ n";  $res = $connection->query (' SELECT ' x ', ' Y ' from ' Test ' LIMIT 1 '); echo "Limit 1:". Memory_get_peak_usage (). "\ n";  $res = $connection->query (' SELECT ' x ', ' Y ' from ' Test ' LIMIT 10000 '); echo "Limit 10000:". Memory_get_peak_usage (). "\ n";

The output results are as follows:

before:224704 Limit 1:224704 Limit 10,000:224,704

Depending on the amount of memory used, everything seems to be fine. In order to be more certain, try to get 100,000 records at a time, the result of the program is the following output:

PHP warning:mysqli::query (): (hy000/2013):        Lost connection to MySQL server during query in/root/test.php on line 11

What's going on here?

The problem is that the MySQL module works in PHP, and the MySQL module is actually a proxy for libmysqlclient. These records are stored directly in memory while the query gets multiple records. Since this memory is not managed by PHP's memory modules, the value obtained by calling the Memory_get_peak_usage () function does not actually use the memory value, so the above problem arises.

We can use MYSQLND instead of MYSQL,MYSQLND to build PHP itself, and its memory usage is controlled by the PHP memory management module. If we use MYSQLND to implement the above code, it will be more realistic to reflect the memory usage situation:

before:232048 Limit 1:324952 Limit 10,000:32,572,912

Even worse, according to PHP's official documentation, the MySQL extended storage query data uses twice times more memory than Mysqlnd, so the original code uses about twice times the memory shown above.

To avoid such problems, consider completing the query several times to reduce the amount of data in a single query:

$totalNumberToFetch = 10000; $portionSize = +;  for ($i = 0; $i <= ceil ($totalNumberToFetch/$portionSize); $i + +) {   $limitFrom = $portionSize * $i;   $res = $connection->query (              "Select ' x ', ' Y ' from ' Test ' LIMIT $limitFrom, $portionSize");}

Contact the above mentioned error 4 can be seen, in the actual coding process, to achieve a balance, can not only meet the functional requirements, but also to ensure performance.

Error 6: Ignoring unicode/utf-8 issues

In PHP programming, when dealing with non-ASCII characters, you will encounter some problems, to be very careful to treat, otherwise it will be wrong everywhere. As a simple example, strlen (name), if name contains non-ASCII characters, the result is somewhat unexpected. Here are some suggestions to try to avoid this type of problem:

If you are not familiar with Unicode and utf-8, then you should at least understand some of the basics. This article is recommended for reading.
It is best to use the mb_* function to handle strings, avoiding the use of old string-handling functions. This is to make sure that PHP's "multibyte" extension is turned on.
Databases and tables are best used with Unicode encoding.
It is known that the Jason_code () function converts non-ASCII characters, but the Serialize () function does not.
PHP code source files are best used in a utf-8 format that does not contain a BOM.
We recommend an article here that describes this type of problem in more detail: UTF-8 Primer for PHP and MySQL

ERROR 7: Assume that $_post always contains post data

$_post in PHP does not always contain data that is submitted by the form post. Suppose we send a POST request to the server through the Jquery.ajax () method:

JS $.ajax ({   URL: ' Http:// ',   method: ' Post ',   data:JSON.stringify ({A: ' A ', B: ' B '}),   contentType: ' Application/json '});

Note The ContentType in the code: ' Application/json ', we are sending data in JSON data format. On the server side, we only output the $_post array:

PHP var_dump ($_post);

You will be surprised to find that the result is as follows:

Array (0) {}

Why is this the result? Our JSON data {A: ' A ', B: ' B '} where is it?

The answer is that PHP only resolves HTTP requests content-type to application/x-www-form-urlencoded or Multipart/form-data. This is because of historical reasons, when PHP first implemented $_post, the most popular is the above two types. So while some types (such as Application/json) are popular today, there is still no automatic processing in PHP.

Because post is a global variable, changing _post is globally valid. So for the request of Content-type to Application/json, we need to parse the JSON data manually and then modify the $_post variable.

PHP $_post = Json_decode (file_get_contents (' Php://input '), true);

At this point, we are going to output the $_post variable, and we will get the output we expect:

Array (2) {["a"]=> string (1) "A" ["B"]=> string (1) "B"}

Error 8: Think PHP supports character data types

Take a look at the following code and guess what it will output:

for ($c = ' a '; $c <= ' z '; $c + +) {   echo $c. "\ n"; }

If your answer is output ' a ' to ' Z ', then you will be surprised to find that your answer is wrong.

Yes, the above code does output ' a ' to ' Z ', but in addition, it will output ' AA ' to ' YZ '. Let's analyze why this is the result.

There is no char data type in PHP, only the string type. Understand this, then the ' Z ' is incremented, the result is ' AA '. For the string comparison size, learn C should know that ' AA ' is less than ' Z '. This also explains why the above output is the result.

If we want to output ' a ' to ' Z ', the following implementation is a good idea:

for ($i = Ord (' A '), $i <= ord (' z '), $i + +) {   echo chr ($i). "\ n"; }

Or This is OK:

$letters = Range (' A ', ' Z ');  for ($i = 0; $i < count ($letters); $i + +) {   echo $letters [$i]. "\ n"; }

Error 9: Encoding standard ignored

Although ignoring the coding standard does not result in errors or bugs, it is important to follow certain coding standards.

There are a lot of problems with your project without a unified coding standard. The most obvious is that your project code is not consistent. The worse part is that your code will be more difficult to debug, extend, and maintain. This also means that your team will be less efficient, including doing a lot of meaningless work.

For PHP developers, it's a bit of a lucky one. Because of the PHP coding standard Recommendation (PSR), it consists of the following 5 parts:

PSR-0: Automatic loading standard
PSR-1: Basic Coding standard
PSR-2: Coding style Guide
PSR-3: Log Interface standard
PSR-4: Auto Load
PSR was originally created and followed by several large groups in the PHP community. Zend, Drupal, Symfony, Joomla and other platforms have contributed to and followed this standard. Even pear, who wanted to be a standard in the early years, has now joined the PSR Camp.

In some cases, it doesn't matter what coding standards you use, as long as you use a coding style and stick with it. But it's a good idea to follow the PSR Standard unless you have a specific reason to do it yourself. More and more projects are now using PSR, and most PHP developers are using PSR, so using PSR will make it easier for new members of your team to become familiar with the project and write code more comfortably.

Error 10: Error using empty () function

Some PHP developers prefer to use the empty () function to make Boolean judgments about variables or expressions, but in some cases they can be confusing.

First, let's take a look at the array arrays in PHP and the Arrayobject object. It looks like there's no difference, it's the same. Is that true?

PHP 5.0 or later: $array = []; Var_dump (Empty ($array));    outputs bool (true) $array = new Arrayobject (); Var_dump (Empty ($array));    outputs bool (FALSE)//Why don ' t these both produce the same output?

To make things more complicated, take a look at the following code:

Prior to PHP 5.0: $array = []; Var_dump (Empty ($array));    outputs bool (false) $array = new Arrayobject (); Var_dump (Empty ($array));    outputs bool (FALSE)

Unfortunately, the above method is very popular. For example, in the Zend Framework 2, Zend\db\tablegateway calls the current () method on the Tablegateway::select () result set to return the DataSet as it did. It's easy for developers to step on the pit.

To avoid these problems, check if an array is empty the last way is to use the count () function:

Note that this is in all versions of PHP (both pre and Post 5.0): $array = []; Var_dump (Count ($array));    outputs int (0) $array = new Arrayobject (); Var_dump (Count ($array));    outputs int (0)

By the way, because PHP will value 0 as the Boolean value false, so the count () function can be directly used in the condition judgment of the IF condition statement to determine whether the array is empty. In addition, the count () function is an O (1) for an array, so using the count () function is a wise choice.

Let's look at an example in which the empty () function is dangerous. When combined with the empty () function in the Magic Method __get (), it is also dangerous. Let's define two classes, each with a test property.

First we define the Regular class, which has a test property:

Class Regular {public   $test = ' value ';}

We then define the Magic class and use the __get () Magic method to access its Test property:

Class Magic {   Private $values = [' Test ' = ' value '];    Public Function __get ($key)   {     if (isset ($this->values[$key]) {       return $this->values[$key];}   } }

All right. Let's take a look at what happens when you access the test property of each class:

$regular = new Regular (); Var_dump ($regular->test);  Outputs string (4) "value" $magic = new Magic (); Var_dump ($magic->test);   Outputs string (4) "Value"

So far, it's normal, and it doesn't make us feel confused.

But what about using the empty () function on the test property?

Var_dump (Empty ($regular->test));  outputs bool (FALSE) Var_dump (empty ($magic->test));   outputs bool (TRUE)

The result is not very surprising?

Unfortunately, if a class uses the Magic __get () function to access the value of a class property, there is no easy way to check whether the property value is empty or does not exist. Outside of the class, you can only check if null values are returned, but this does not necessarily mean that the corresponding key is not set because the key value can be set to null.

In contrast, if we access a non-existent property of the Regular class, we get a notice message similar to the following:

notice:undefined property:regular:: $nonExistantTest in/path/to/test.php on the call  Stack:   0.0012   234704  1. {main} ()/path/to/test.php:0

Therefore, for the empty () function, we should be careful to use, otherwise it will result unexpectedly, even potentially misleading you.

A PHP programming Problem: Suppose ABCDEFGHIJ 10 letters represent 1,234,567,890 digits

function zh ($x) {
$num =strlen ($x);
$str = ' Abcdefghij ';
for ($i =0; $i < $num; $i + +) {
$arr []=substr ($x, $i, 1);
$zhstr. = $str [$arr [$i]];
return $zhstr;

$str =zh (' 1563 '); Set the initial value
Print ($STR);

PHP Programming issues

(1) if ($select) is abbreviated. Same as if ($select ==true) effect. The IF () determines whether the expression in parentheses is true or false and does not need to be ==true.
(2) This custom function, is actually a bubble sort, the principle is that if there is a length of 10 of the array, first compare the number of adjacent, the largest value to the tenth place, and then from the beginning to compare the remaining 9, 9 of the largest in the Nineth place, continue to compare the remaining 8, pick the largest eighth bit.
The outer layer for the loop function is every time from the beginning, let the inner loop compare 10, 9, 8 ....
(3) strcmp The ASCII code of the same bit from left to right, z>a>z>a>9>1>. As soon as one detects a certain inequality, the 0,-1,1 is drawn.
And strnatcmp in the NAT is the abbreviation of "natural", what is natural, 2<10 this is natural, but if strcmp will be in accordance with the ASCII code line, will draw "2" > "10" true techarticle Simple analysis of PHP programming 10 most common errors, analysis of PHP programming 10 currently learning PHP a lot of friends, in peacetime routine development projects always encounter a variety of problems, this experience will ...

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