Home Backend Development PHP7 What are the differences between php7 and php5

What are the differences between php7 and php5

May 18, 2021 am 09:37 AM

This article will introduce to you the difference between php7 and php5. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What are the differences between php7 and php5

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 operator

// 整型比较
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); // PHP_EOL 为换行符
//结果:
0
-1
1
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, which means binding an anonymous function to the class

<?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()可以定义数组
<?php
session_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. The variable storage bytes are reduced, reducing memory usage and improving variable operation speed.

2. Improving the array structure. The array elements and hash mapping table are allocated in the same memory, reducing the Reduce memory usage and improve CPU cache hit rate

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

Recommended learning:php video tutorial

The above is the detailed content of What are the differences 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to Use Sessions Effectively in PHP 7? How to Use Sessions Effectively in PHP 7? Mar 10, 2025 pm 06:20 PM

This article details effective PHP 7 session management, covering core functionalities like session_start(), $_SESSION, session_destroy(), and secure cookie handling. It emphasizes security best practices including HTTPS, session ID regeneration, s

How to Monitor PHP 7 Performance with Tools like New Relic? How to Monitor PHP 7 Performance with Tools like New Relic? Mar 10, 2025 pm 06:28 PM

This article explains how to monitor PHP 7 application performance using New Relic. It details New Relic's setup, key performance indicators (KPIs) like Apdex score and response time, bottleneck identification via transaction traces and error track

How to Upgrade from PHP 5.6 to PHP 7? How to Upgrade from PHP 5.6 to PHP 7? Mar 10, 2025 pm 06:29 PM

This article details upgrading PHP 5.6 to PHP 7, emphasizing crucial steps like backing up, checking server compatibility, and choosing an upgrade method (package manager, compiling, control panel, or web server configuration). It addresses potentia

How to Autoload Classes in PHP 7? How to Autoload Classes in PHP 7? Mar 10, 2025 pm 06:20 PM

This article explains PHP 7's autoloading, using spl_autoload_register() to load classes on demand. It details best practices like namespace-based autoloading and caching for performance optimization, addresses common issues (e.g., class not found

How to Use Git for Version Control in PHP 7 Projects? How to Use Git for Version Control in PHP 7 Projects? Mar 10, 2025 pm 06:27 PM

This article guides PHP 7 developers on using Git for version control. It covers initialization, staging, committing, ignoring files, remote repositories, branching, merging, conflict resolution, and essential Git commands. Best practices for effic

How to Deploy a PHP 7 Application to a Web Server? How to Deploy a PHP 7 Application to a Web Server? Mar 10, 2025 pm 06:28 PM

This article details deploying PHP 7 applications, covering methods (FTP, SSH, deployment tools), server configuration (Apache/Nginx, PHP-FPM), database setup, and crucial security considerations. It highlights common challenges like server compatib

How to Use Xdebug for Debugging PHP 7 Code? How to Use Xdebug for Debugging PHP 7 Code? Mar 10, 2025 pm 06:26 PM

This article explains how to use Xdebug for debugging PHP 7 code. It covers Xdebug configuration (installation, php.ini settings, IDE setup), breakpoint usage (conditional, function, remote), and troubleshooting connection issues. Effective debuggi

How to Include and Require Files in PHP 7? How to Include and Require Files in PHP 7? Mar 10, 2025 pm 02:52 PM

This article explains PHP 7's include, include_once, require, and require_once file inclusion methods. It details their differences in error handling (warnings vs. fatal errors) and multiple inclusion prevention. Best practices for file organizatio

See all articles