PHP7-0 New Features
Variable Types PHP 7 added type constraints for function parameters and return values. Why did PHP add types? In fact, this feature was preparation for the JIT feature in PHP 7.1. With types added, PHP JIT can accurately determine variable types and generate the optimal machine instructions. (Optimization for compute-intensive operations) Usage example function test(int $a, string $b, array $c) : int { //code } Error Exceptions In the past, PHP programs
目录14 个章节
Variable Types
PHP 7 added type constraints for function parameters and return values. Why did PHP add types? In fact, this feature was preparation for the JIT feature in PHP 7.1. With types added, PHP JIT can accurately determine variable types and generate the optimal machine instructions. (Optimization for compute-intensive operations)
Usage example
function test(int $a, string $b, array $c) : int {
//code
}
Errors and Exceptions
In the past, after a PHP program encountered an error, the Zend engine would raise a fatal error and terminate the program. PHP 7 can use try/catch to catch errors.
try{
not_exists_func();
}catch(EngineException $e){
echo "Exception: {$e->getMessage()}\n";
}
PHP 7 Performance Optimizations
zval uses stack memory
In the Zend engine and extensions, a PHP variable often needs to be created, which at the bottom is a zval pointer.
Previous versions dynamically allocated zval memory from the heap via MAKE_STD_ZVAL, while PHP 7 can use stack memory directly.
Benefit: saves the cost of memory allocation
Underlying code
//php5
zval *val;
MAKE_STD_ZVAL(val);
//php7
zval val;
zend_string stores the hash value; array lookups no longer need to recompute the hash
PHP 7 created a new type specifically for strings called zend_string. In addition to a char * pointer and length,
it added a hash field to store the string's hash value.
Array key lookups no longer need to repeatedly compute the hash value.
struct _zend_string{
zend_refcounted gc;
zend_ulong h;
size_t len;
char val[1];
};
Data is stored directly in hashtable buckets, reducing the number of memory allocations and improving Cache hit rate and memory access speed
In previous versions of PHP, every time data was inserted into an array, memory had to be allocated once, and the memory was stored on different memory pages, which lowered the hit rate on access.
zend_parse_parameters changed to a macro implementation, 5% performance improvement
Improves PHP extension performance
4 new OPCODEs added: call_user_function, is_int/string/array, strlen, defined — these 4 functions became PHP OpCode instructions, making them faster
Other performance optimizations
- Basic types such as
int,float,boolchanged to direct value copy - Sorting algorithm improvements
PCRE with Jit(regular expressions)execute_dataandoplineuse global registers- Use the
PGOfeature ofgdb 4.8
PHP 7 JIT
The PHP 7.0-final version will not include the JIT feature
JIT is the abbreviation of Just in Time, meaning converting instructions to binary machine code at runtime. For compute-intensive programs, JIT can convert PHP OpCode directly into machine code, greatly improving performance. The PHP development team restarted the JIT development plan themselves, and PHP 7.1 is expected to include the JIT feature
Why PHP 7.0 does not have JIT
JIT does not provide much of a performance improvement for real-world projects such as WordPress
PHP Built-in Development Test Server
cd ~/php
php -S 0.0.0.0:8080
This starts a server with ~/php as the website root directory.
About

For more interesting tips, follow my WeChat official account: DealiAxy Every article is also collected on my blog: blog.deali.cn
_EOF_
PHP7-0 New Features
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
PHP7-0-+-Swoole-环境配置
前言 Swoole是一个面向生产环境的 PHP 异步网络通信引擎。 使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,
2021-12-18 · PHP · 约 1 分钟
PHP-Ajax-跨域问题最佳解决方案
前言 Ajax跨域问题在开发中非常常见。 例如网页的域名是client.abc.com,而请求的域名是server.abc.com。 如果直接使用ajax访问,会有以下错误: XMLHttpRequest cannot load http://server.abc.com/server.php. No 'Access-Control-Allow-Origin' header is present
2021-12-18 · PHP · 约 1 分钟
2023年的PHP项目部署笔记。什么?还有人用PHP?
PHP?2023年还在用?Docker部署心累,不如直接装php-fpm和nginx省心。权限问题?chown一下搞定,composer安装依赖完美运行。
2023-12-12 · PHP · 约 1 分钟
code-porter 代码工作区迁移工具
换电脑几十个项目手动拷?你不是懒就是蠢。写了code-porter,自动扫描、智能打包(bundle/zip),脏仓库也能保留未提交改动,一行uvx搞定。零依赖跨平台,U盘一插完事。别问我为什么不用rsync——它懂你项目结构?
2026-05-22 · DevOps · 约 1 分钟
讨论
共 0 条加载评论中…