When switching to PHP development, some common pits

Note: Subsequent new pits will be explained in the front

10. The problem of filesize cache

The filesize of php is actually cached (of course there are many more, here is only filesize as an example, other functions that will be cached, subject to the official document) The
online code often encounters various problems randomly, after a month of investigation, the online addition Various logs, and finally found that the problem of filesize cache, the following code:

[php] view plain copy

  1. echo filesize("a.txt");  
  2. exec("rm a.txt"); // delete the file  
  3. echo filesize("a.txt"); // the size will be output here instead of an error saying the file does not exist  
  4.   
  5. echo filesize("b.txt");  
  6. echo filesize("a.txt"); // This will report an error that the file does not exist, because only the last file is cached, there is only b in the cache, and there is no cache for a  

See, filesize not only caches, but also only caches the last file, so PHP developers don't know how to think about it, so they won't add a filesize_withcache method?
Knowing the reason, the solution is simple, clear the cache before the filesize call, and add the code: clearstatcache()
Refer to the official documentation: http://php.net/manual/zh/function.filesize.php
 

 

9. When saving source files, pay attention to use utf-8 without BOM signature

I used Notepad on Windows to edit the file, and after publishing it on Linux, I kept getting an error: Cannot modify header information - headers already sent by (output started at xxx.php:1)
I couldn't find the problem by comparing the code with Winmerge or BeyondCompare. Later I used Netbeans found that there is an invisible character at the top of the file, and after researching it, I found out that it is the Bom signature of Windows.
That is to say, if you develop on Windows and publish on Linux, you should use an editor that does not support Bom. If you use VisualStudio, you need to Select advanced save option without signature

 

1. Comparison of null and empty, 0, false and other four values

In PHP, == will perform type conversion first, and then compare, and === will compare types first. If the types are different, the direct return is not equal, refer to the following example

[php] view plain copy

  1. $a = null; $b = ''; $c = 0; $d = false;  
  2. echo ($a == $b)?1:0; // output 1  
  3. echo ($a === $b)?1:0; // output 0  
  4. echo ($a == $c)?1:0; // output 1  
  5. echo ($a === $c)?1:0; // output 0  
  6. echo ($b == $c)?1:0; // output 1  
  7. echo ($b === $c)?1:0; // output 0  
  8. echo ($a == $d)?1:0; // output 1  
  9. echo ($a === $d)?1:0; // output 0  

For a code farmer like me who only wrote js or C# code before, I have been fooled by these values ​​n times, and n is greater than 3

 

 

 

2. strrchr function

A note on the W3School site reads:

The strrchr() function finds the position of the last occurrence of a string within another string and returns all characters from that position to the end of the string.
If successful, false otherwise.
 

In fact, this function is to find a character, not a string, you should refer to the official documentation

Code example:

[php] view plain copy

  1. $a = 'abcdef.txt';  
  2. $b = '.php';  
  3. echo strrchr($a, $b);  

The output of the above code is: .txt

That is, if $b is a string, only the first character is used, other characters after it are ignored

Note: php provides the strstr function, why not provide the strrstr function, although it is very simple to implement it yourself

 

 

3. Reference assignment in foreach , see official documentation

This reference assignment is very good. For me, who uses C#, it is impossible to modify the foreach element in C#, and an exception will occur. PHP makes this possible, but:
there is a warning in the official document. :Warning The $value reference to the last element of the array remains after the foreach loop. It is recommended to use unset() to destroy it.
Let's look at a set of code:

[php] view plain copy

  1. $a = [1,2,3];  
  2. foreach($a as &$item){  
  3.     echo $item . ',';  
  4. }  
  5. //unset($item); // object is not destroyed after reference assignment  
  6. foreach($a as $item){  
  7.     echo $item . ',';  
  8. }  

The output of the above code is as follows:
1,2,3,1,2,2 The last output is 2, not 3, because $item is not destroyed in the code. The reason is as follows:
the first loop, put The reference of 3 is assigned to $item, and the second loop assigns 1 to $item, because $item is a reference, causing element 3 of the array to become 1, understand?

 

4. The connection and difference between isset and empty , isset document  empty document
empty returns true for the following 8 cases:
  null, empty string "", string 0 "0", empty array, boolean value false, number 0, floating point number 0.0 , the class is defined with var but not assigned

isset detects whether the variable is set, and it is not NULL, but for the 8 cases of empty, only null returns false, and the other 7 cases return true

To sum up, except for the 7 non-null cases described by empty, in other cases, if(empty(variable)) is equivalent to if(!isset(variable))

One flexible usage: directly accessing $arr['aaa'] may report an error, saying that aaa does not exist, you can use:
if(isset($arr['aaa']){operation code} or if(!empty($arr[ 'aaa']){action code}

 

5. When the trim function encounters Chinese spaces, it will be garbled

[php] view plain copy

  1. $str = ' "Half-width spaces before and after" ';  
  2. var_dump ($ str);  
  3. $str2 = trim($str, '  ');  
  4. var_dump ($ str2);  
  5. $str3 = mb_ereg_replace('^(?:\s| )+|(?:\s| )+$', '', $str);  
  6. var_dump ($ str3);  
  7. $str4 = mb_ereg_replace('^[\s ]+|[\s ]+$', '', $str);  
  8. var_dump ($ str4);  

Refer to the above code and output the result:

[plain] view plain copy

  1. string ' "Half-width spaces before and after" ' (length=38)  
  2. string '�There are full-width spaces before and after"' (length=28)  
  3. string '"There are full-width spaces before and after"' (length=30)  
  4.   
  5. A PHP Error was encountered  
  6. Severity: Warning  
  7. Message: mb_ereg_replace(): mbregex compile err: invalid code point value  

It can be seen that: trim causes garbled characters to appear, the regular ^(?:\s| )+|(?:\s| )+$ can work normally, while the regular ^[\s ]+|[\s ]+$ does not Compile exception, the reason I haven't


found 6. Intval is on Windows and Centos, and the maximum value range is different.
On centos, the maximum value of intval conversion is 9223372036854775807,

On my Win7x64+64-bit php, the maximum conversion value is 2147483647, which is very strange. The manual clearly states that 64-bit systems are all 9223372036854775807.
Such a big pit, the official explanation is that the following php6 is only a beta version on win, 64-bit is not supported

 

7. When the mysql field type is varchar, it cannot be retrieved with where xx=123, and the index cannot be used. It is
recommended that all SQL values ​​be enclosed in single quotes, such as: where xx='123', if the field type is int, also proper use of indexes

 

8. Do not judge the return value after executing SQL, or judge the logic error:

 

$sql = 'insert into app_log(id) select 0 from dual where 1=2';
$this->db->query($sql);
return true; // return true without judgment

$sql = 'insert into app_log(id) select 0 from dual where 1=2';
$result = $this->db->query($sql);
if ($result) { // There is a bug, the insertion is unsuccessful , the result is also true
    return true;
}
return false;

The 2 pieces of code above should be changed to:

$sql = 'insert into app_log(id)  select 0 from dual where 1=2';
$this->db->query($sql);
// 如果sql有语法问题,affected_rows是-1
if ($this->db->affected_rows() > 0) {
    return true;
}
return false;

Note: In mysql, update a set name='123' where id=1;
if the record with id 1 and name is already 123, then affected_rows()=0 of this update statement

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325309248&siteId=291194637