Home  >  Article  >  Backend Development  >  Detailed explanation of operations that PHP developers almost always make mistakes

Detailed explanation of operations that PHP developers almost always make mistakes

黄舟
黄舟Original
2018-05-29 10:27:561588browse

1. Preface

Recently, I have encountered some minor problems that are not painful or itchy due to PHP doing mathematical operations.

A thousand-mile embankment collapses in an ant nest. Adding a type conversion solves it so easily, but I don't think we can just let it go.

Especially use PHP to do financial calculations or write interface calculations and strong language docking students should pay more attention.

It’s not a big deal, details determine success or failure. After careful study, there are indeed many tips, and I also learned a lesson.

Do you really know that php is a weakly typed language?

Some time ago, I conducted research on the PHP kernel and had a detailed understanding of the underlying storage structure of PHP variables. However, I did not understand the operation process of different types of values ​​and the changes in variable types. process.

In fact, it is a problem with our intelligent PHP [automatic type conversion]. This is also the power of PHP as a weakly typed language., simply Study it completely and make a summary.

(There are 5 examples below, all are very simple operations, but you may not be able to tell the reason)

2. Process Analysis

Case 1

Let’s first look at the problems I encountered (simplified) , which is the trigger for me to write this blog.

  $a = '1.11';
  $b = '0.11';
  var_dump($a);//string(4) "1.11" 
  var_dump($b);//string(4) "0.11" 
  $re = $a - $b;
  var_dump($re);//float(1)

Note: Two changes have occurred.
1. Subtract strings and turn them into floating point types
2. The minuends are all two decimal places, and the result is no decimal [This is also where the bug occurs, because the app requires two decimal places for display 】
Similarly, when subtracting numbers without decimals in strings, the result is int

 $a = '11';
  $b = '1';
  var_dump($a);//string(4) "11" 
  var_dump($b);//string(4) "1" 
  $re = $a - $b;
  var_dump($re);//int(10)

Conclusion:
1. In the process of PHP's underlying operation , type conversion will be performed automatically, decimals are converted to float, and integers are converted to int.
2. If you need to limit the number of decimal places for numbers, remember to deal with it. number_format();

has already started, let’s talk about this type conversion.

Example 2
Question: Is the following true or false

    var_dump(0123 == 123);  
    var_dump('0123' == 123);  
    var_dump('0123' === 123);

What is the answer? ?
false;true;false
Analysis:
I believe it is easy for everyone to guess that the third one is false, because when === the strong judgment is made, type comparison is added
There are two things to pay attention to here point. On the one hand, the bottom layer of PHP will consider the integer numbers starting with 0 to be octal; on the other hand, when converting sting to int, the leading 0 will be removed
var_dump(0123 == 123); // false, PHP will default to 0123 is treated as octal, but actually converted to decimal is 83. Obviously this is not equal.
var_dump('0123' == 123); // true here php will be very interesting to convert '0123' into a number and remove the previous 0 by default, which is 123==123
var_dump('0123 ' === 123); // false Obviously, the above question has said that the number and string types are inconsistent.
Conclusion:
1. Integer numbers starting with 0 will be processed by PHP as octal
2. Conclusion 1 of colleague Example 1, strings will automatically perform type conversion during operation , and the leading 0 will be removed


Example 3
What is the result of $x below:

      $x = NULL;
      if ('0xFF' == 255) {
          $x = (int)'0xFF';
      }
      $x = ?

    答案是什么呢??
      $x=0而不是255
    注意点:
      首先'oxFF' == 255我们好判断,会进行转换将16进制数字转换成10进制数字,0xff = 255。PHP使用is_numeric_string 判断字符串是否包含十六进制数字然后进行转换。
      但是$x = (int)'0xFF';是否也会变成255呢?显然不是,将一个字符串进行强制类型转换实际上用的是convert_to_long,它实际上是将字符串从左向右进行转换,遇到非数字字符则停止。因此0xFF到x就停止了。所以$x=0
    结论:
      1.0开头的整形数字PHP会当作十六进制来处理
      2. string->int的过程,是将字符串从左向右进行转换,遇到非数字字符则停止。

事例四
  经过下面的运算 $x的值应该是多少?

  $x = 3 + "15%" + "$25"

  答案是什么呢?? 18
  注意点:其实就是前边的所提到的点。3+15+0=18(0时因为从左往右取数字嘛,遇到非数字停止,没有当然为0)
事例五(无关类型转换,但也很有意思)

 $a = true && false;
  var_dump($a);
  $a = true and false;
  var_dump($a);

  答案是什么呢??
    false;true

  为什么呢?是对运算符优先级的一个理解,哈哈,提醒到这里自己去查查吧~

事例六

  $arr = array(0,1,2,3);
  foreach ($arr as $key => $value) {}
  var_dump(current($arr));//最后指针停留在数组结尾,取不到值了输出false

  $arr = array(0,1,2,3);
  foreach ($arr as $key => $value) { 
  //$arr其实是进行了一次传值,用的是$arr_copy 
        $arr[$key] = $value;//进行了改值,则发生分离现象
  }
  var_dump(current($arr));//输出1

输出false 与 1;(PHP5.6环境下,php7已经做了修改);

那这个又是为什么呢?【和PHP内核有关,变量分离改变】

The above is the detailed content of Detailed explanation of operations that PHP developers almost always make mistakes. For more information, please follow other related articles on the PHP Chinese website!

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