Home Backend Development PHP7 Look at the difference between php7 and php5

Look at the difference between php7 and php5

Jan 06, 2021 am 09:12 AM
php5 php7

PHP7 column will show you the differences between php7 and php5

Look at the difference between php7 and php5

# #Recommended (free): PHP7

1, php scalar type and return type declaration

#主要分为两种模式,强制性模式和严格模式declare(strict_types=1)#1表示严格类型校验模式,作用于函数调用和返回语句;0表示弱类型校验模式。
Copy after login

2, NULL coalescing operator

$site = isset($_GET['site']) ? $_GET['site'] : 'wo';#简写成$site = $_GET['site'] ??'wo';
Copy after login

3. Combination budget symbol

// 整型比较print( 1 <=> 1);print(PHP_EOL);print( 1 <=> 2);print(PHP_EOL);print( 2 <=> 1);print(PHP_EOL);print(PHP_EOL); // PHP_EOL 为换行符//结果:0-11
Copy after login

4. Constant array

// 使用 define 函数来定义数组define(&#39;sites&#39;, [
   &#39;Google&#39;,
   &#39;Jser&#39;,
   &#39;Taobao&#39;]);print(sites[1]);
Copy after login

5. Anonymous class

interface Logger { 
   public function log(string $msg); } class Application { 
   private $logger; 

   public function getLogger(): Logger { 
      return $this->logger; 
   } 

   public function setLogger(Logger $logger) { 
      $this->logger = $logger; 
   }   } $app = new Application; // 使用 new class 创建匿名类 $app->setLogger(new class implements Logger { 
   public function log(string $msg) { 
      print($msg); 
   } }); $app->getLogger()->log("我的第一条日志");
Copy after login

6. The Closure::call() method is added, meaning it is bound to the class Define an anonymous function

<?php class A { 
    private $x = 1; } // PHP 7 之前版本定义闭包函数代码 $getXCB = function() { 
    return $this->x; }; // 闭包函数绑定到类 A 上 $getX = $getXCB->bindTo(new A, &#39;A&#39;);  echo $getX(); print(PHP_EOL); // PHP 7+ 代码 $getX = function() { 
    return $this->x; }; echo $getX->call(new A); ?>
Copy after login

7, CSPRNG (pseudo-random number generator).

PHP 7 通过引入几个 CSPRNG 函数提供一种简单的机制来生成密码学上强壮的随机数。random_bytes() - 加密生存被保护的伪随机字符串。random_int() - 加密生存被保护的伪随机整数。
Copy after login

8, Exception

PHP 7 异常用于向下兼容及增强旧的assert()函数。
Copy after login

9, Use statement change

#可以导入同一个namespace下的类简写use some\namespace\{ClassA, ClassB, ClassC as C};
Copy after login

10, Session option

1.session_start()可以定义数组<?phpsession_start(&#91;
   &#39;cache_limiter&#39; => &#39;private&#39;,
   &#39;read_and_close&#39; => true,]);?>2.引入了一个新的php.ini设置(session.lazy_write),默认情况下设置为 true,意味着session数据只在发生变化时才写入。
Copy after login

11, PHP 7 removed extension

ereg

mssql

mysql

sybase_ct
Copy after login

Why is the performance of PHP7 improved compared to PHP5?

1. Reduced variable storage bytes, reducing memory usage, and improving variable operation speed

2. Improved array structure, array elements and hash mapping table are allocated in the same memory Here, the memory usage is reduced and the CPU cache hit rate is improved

3. The function calling mechanism is improved. By optimizing the parameter transfer link, some instructions are reduced and the execution efficiency is improved

For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of Look at the difference between php7 and php5. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1652
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles