Home > Backend Development > PHP7 > New features of php7: changes brought by php7

New features of php7: changes brought by php7

L
Release: 2023-02-17 15:32:01
forward
2676 people have browsed it

New features of php7: changes brought by php7

Changes brought by PHP7

1. String processing mechanism modification

Strings containing hexadecimal characters are no longer regarded as numbers, nor are they treated differently.

var_dump("0x123" == "291"); // false
var_dump(is_numeric("0x123")); // false
var_dump("0xe" + "0x1"); // 0
var_dump(substr("f00", "0x1")) // foo
Copy after login

2. Integer processing mechanism modification

Int64 support, unifies the integer length under different platforms, and supports strings and file uploads larger than 2GB. 64-bit PHP7 string length can exceed 2^31 bytes.

// 无效的八进制数字(包含大于7的数字)会报编译错误
$i = 0681; // 老版本php会把无效数字忽略。
// 位移负的位置会产生异常
var_dump(1 >> -1);
// 左位移超出位数则返回0
var_dump(1 << 64);// 0
 // 右位移超出会返回0或者-1
var_dump(100 >> 32);// 0 
var_dump(-100 >> 32);// -1
Copy after login

3. Parameter processing mechanism modification

Does not support repeated parameter naming

function func(b, c) {} ; Will report an error

The two methods func_get_arg() and func_get_args() return the current value of the parameter, not the value passed in, and the current value may be modified

So it should be noted that it is best to record it in the first line of the function, otherwise if there is any subsequent modification, it will not be the initial value passed in when read again.

function foo($x) {
$x++;
echo func_get_arg(0);
}
foo(1); //返回2
Copy after login

4.foreach modification

foreach() loop no longer works on array internal pointers

$arr = [1,2,3];
foreach ($arr as &$val) {
echo current($arr);// php7 全返回0
}
Copy after login

When looping according to the value, foreach is a copy operation of the array

$arr = [1,2,3];
foreach ($arr as $val) {
unset($arr[1]);
}var_dump($arr);
Copy after login

The latest php7 will still print out [1,2,3]. (ps: 7.0.0 does not work)
The old one will print out [1,3]

When looping according to the reference, modifications to the array will affect the loop

$arr = [1];
foreach ($arr as $val) {
var_dump($val);
$arr[1]=2;
}
Copy after login

The latest php7 will still add a cycle of new elements. (ps: 7.0.0 does not work)

5. List modification

no longer assigns values ​​in the reverse order

/ /$arr will be [1,2,3] instead of the previous [3,2,1]

list($arr[], $arr[], $arr[]) = [1,2,3];
Copy after login

no longer supports string splitting function

// $x = null 并且 $y = null
$str = &#39;xy&#39;;
list($x, $y) = $str;
Copy after login

Empty list() assignment is no longer allowed

list() = [123];
Copy after login

list() now also works for array objects

list($a, $b) = (object)new ArrayObject([0, 1]);
Copy after login

6 .Variable handling mechanism modification

Indirect calls to variables, properties and methods will now be parsed strictly in left-to-right order, instead of being mixed with several special cases. Condition. The table below illustrates this change in parsing order.

New features of php7: changes brought by php7

The order of array elements or object attributes automatically created during reference assignment is different from before

$arr = [];
$arr[&#39;a&#39;] = &$arr[&#39;b&#39;];$arr[&#39;b&#39;] = 1;
// php7: [&#39;a&#39; => 1, &#39;b&#39; => 1]
// php5: [&#39;b&#39; => 1, &#39;a&#39; => 1]
Copy after login

7. Miscellaneous

1.debug_zval_dump() now prints "int" instead of "long", and prints "float" instead of "double"

2.dirname() added optional The second parameter, depth, gets the name of the parent directory one depth level up from the current directory.

3.getrusage() now supports Windows.mktime() and gmmktime() functions no longer accept the is_dst parameter.

4. The preg_replace() function no longer supports "\e" (PREG_REPLACE_EVAL). Preg_replace_callback() should be used instead.

5. The setlocale() function no longer accepts the category input string. The LC_* constants should be used.

6.exec(), system() and passthru() functions have added protection for NULL.

7.shmop_open() now returns a resource instead of an int. This resource can be passed To shmop_size(), shmop_write(), shmop_read(), shmop_close() and shmop_delete().

8. To avoid memory leaks, xml_set_object() now requires manual clearing of $parse at the end of execution.

9.curl_setopt setting item CURLOPT_SAFE_UPLOAD change

TRUE disables the @ prefix for sending files in CURLOPT_POSTFIELDS. This means @ can be safely used in fields. CURLFile can be used as an upload alternative.
Added in PHP 5.5.0, default value is FALSE. PHP 5.6.0 changes the default value to TRUE. . PHP 7 removed this option and you must use the CURLFile interface to upload files.

Recommended tutorial: "PHP7 Tutorial"

The above is the detailed content of New features of php7: changes brought by php7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template