Home > Backend Development > PHP Problem > Is it necessary to upgrade php?

Is it necessary to upgrade php?

(*-*)浩
Release: 2023-02-26 18:42:01
Original
3151 people have browsed it

Since php upgrade is a top priority, the company only plans to upgrade in the near future, so before I could only appreciate the pleasure brought by php7 in private

Is it necessary to upgrade php?

Benefits brought by PHP7

Yes, the performance has been greatly improved, which can save machines and save money. (Recommended learning: PHP video tutorial)

Is it necessary to upgrade php?

New things brought by PHP7

1. Type declaration.

You can use string (string), integer (int), floating point number (float), and Boolean value (bool) to declare the parameter type and function return value of the function.

declare(strict_types=1);
function add(int $a, int $b): int {
    return $a+$b;
}
echo add(1, 2);
echo add(1.5, 2.6);
Copy after login

php5 cannot execute the above code. When php7 is executed, it will first output a 3 and an error (Argument 1 passed to add() must be of the type integer, float given);

There are two modes for scalar type declarations: mandatory (default) and strict mode.

declare(strict_types=1), must be placed in the first line of the file to execute the code, the current file is valid!

2.set_exception_handler() no longer guarantees that what is received must be an Exception object

In PHP 7, many fatal errors and recoverable fatal errors have been Converted to exception to handle. These exceptions inherit from the Error class, which implements the Throwable interface (all exceptions implement this base interface).

PHP7 further facilitates developers' processing and allows developers to have greater control over the program. Because by default, Error will directly cause the program to interrupt, while PHP7 provides the ability to capture and process it, allowing the program to continue. The implementation continues to provide programmers with more flexible options.

3. New operator "<=>"

语法:$c = $a <=> $b
Copy after login

If $a > $b, the value of $c is 1

If $a == $b, the value of $c is 0

If $a < $b, the value of $c is -1

4.New Operator "??"

If the variable exists and the value is not NULL, it will return its own value, otherwise it will return its second operand.

//原写法
$username = isset($_GET[&#39;user]) ? $_GET[&#39;user] : &#39;nobody&#39;;
//现在
$username = $_GET[&#39;user&#39;] ?? &#39;nobody&#39;;
Copy after login

5.define() Define constant array

define(&#39;ARR&#39;,[&#39;a&#39;,&#39;b&#39;]);
echo ARR[1];// a
Copy after login

6.AST: Abstract Syntax Tree, abstract syntax tree

AST plays the role of a middleware in the PHP compilation process, replacing the original method of spitting out opcode directly from the interpreter, decoupling the interpreter (parser) and the compiler (compliler), which can reduce some Hack codes and make the implementation more efficient. Easy to understand and maintainable.

PHP5: PHP code-> Parser syntax analysis-> OPCODE-> Execution

PHP7: PHP code-> Parser syntax analysis-> AST -> OPCODE -> Execute

7. Anonymous function

$anonymous_func = function(){return &#39;function&#39;;};
echo $anonymous_func(); // 输出function
Copy after login

8. Unicode character format support (echo “\u{9999}”)

9.Unserialize provides filtering features

Prevents code injection of illegal data and provides safer deserialized data.

10. Namespace reference optimization

// PHP7以前语法的写法 
use FooLibrary\Bar\Baz\ClassA; 
use FooLibrary\Bar\Baz\ClassB; 
// PHP7新语法写法 
use FooLibrary\Bar\Baz\{ ClassA, ClassB};
Copy after login

The above is the detailed content of Is it necessary to upgrade php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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