php7与php5的区别,PHP7和PHP5区别
PHP7比PHP5.0 ~ 5.6快了近5倍,同時還降低了對系統資源的占用。主要是PHP7對Zend引擎進行了深度優化。
之前在書上看到過兩者比較全面的區別,回頭去翻了翻書【眼過千遍,不如手過一遍啊】,特記錄如下。
分三個方面來比較:
0、底層優化
1、語法變化
2、性能變化
0、增加抽象語法樹(AST)。
PHP5:PHP代碼在語法解析階段直接生成opline指令,執行器直接執行opline指令。
PHP7: PHP代碼解析生成抽象語法樹,然后將抽象語法樹編譯成opline指令。解耦編譯器和執行器。
1、Native TLS(線程局部存儲)
PHP5: 通過參數傳遞的方式傳遞本線程資源池。容易遺漏、并且及其不優雅。
PHP7: 使用Native TLS來保存線程的資源池。
2、指定函數參數、返回值類型
PHP7: 指定了函數參數和返回值類型。
注意??:如果類型不一致會報error錯誤。嘗試了不太好用,比如int和string不好分,input過來的id=1是string, 代碼中是int, 需要手動轉換。
3、zval結構的變化。
PHP5: refcount__gc在zval中;zval結構大小24byte。
PHP7: refcount__gc在zval的value中;zval結構大小16byte。
3.1、PHP5 zval結構
定義文件{PHPSRC}/Zend/zend.h
typedef struct _zval_struct {
zvalue_value value;
zend_uint refcount__gc;
zend_uchar type;
zend_uchar is_ref__gc;
} zval;
typedef union _zvalue_value {
long lval;
double dval;
struct {
char *val;
int len;
} str;
HashTable *ht;
zend_object_value obj;
} zvalue_value;
typedef struct _zval_struct zval;
struct _zval_struct {
zend_value value;/* value */
union {
struct {
ZEND_ENDIAN_LOHI_3(
zend_uchar type,/* active type */
zend_uchar type_flags,
union {
uint16_t extra; /* not further specified */
} u)
} v;
uint32_t type_info;
} u1;
union {
uint32_t next; /* hash collision chain */
uint32_t cache_slot; /* cache slot (for RECV_INIT) */
uint32_t opline_num; /* opline number (for FAST_CALL) */
uint32_t lineno; /* line number (for ast nodes) */
uint32_t num_args; /* arguments number for EX(This) */
uint32_t fe_pos; /* foreach position */
uint32_t fe_iter_idx; /* foreach iterator index */
uint32_t access_flags; /* class constant access flags */
uint32_t property_guard; /* single property guard */
uint32_t constant_flags; /* constant flags */
uint32_t extra; /* not further specified */
} u2;
};
typedef union _zend_value {
zend_long lval;/* long value */
double dval;/* double value */
zend_refcounted *counted;
zend_string *str;
zend_array *arr;
zend_object *obj;
zend_resource *res;
zend_reference *ref;
zend_ast_ref *ast;
zval *zv;
void *ptr;
zend_class_entry *ce;
zend_function *func;
struct {
uint32_t w1;
uint32_t w2;
} ww;
} zend_value;
4、異常處理
PHP7將多數錯誤改為異常拋出。
PHP5: 調用的方法不存在報error。
PHP7: 可通過try catch捕獲,使得錯誤處理更加可控。
try {
salmonl();
} catch (Throwable $e) {
var_dump($e->getMessage);
}
// PHP5下運行結果
// Fatal error: Call to undefined function salmonl() in [...][...] on line 3
// PHP7下運行結果
// Notice: Undefined property: Error::$getMessage in [...][...] on line 5
說明:PHP7下是通過Throwable異常類來捕獲的,Throwable是父類,Exception類和Error類是子類。
5、HashTable的變化。
PHP5: HashTable結構大小72tye; 數組元素Bucket結構72byte。
PHP7: HashTable結構大小56tye; 數組元素Bucket結構32byte。
6、執行器。
PHP5: 通過執行器的調度函數execute_ex()傳遞execute_data和opline兩個變量。
PHP7: execute_data和opline通過寄存器變量存儲。
7、新的參數解析方式。
PHP7: 定義2個宏包裹待解析參數。
8、語法新特性
8.0、null合并運算符
// PHP5
$id = isset($_GET['id']) ? $_GET['id'] : 0;
// PHP7
$id = $_GET['id'] ?? 0;
8.1、太空船操作符
// PHP5
usort($datas, function ($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] < $b['score']) ? 1 : -1;
});
// PHP7
usort($datas, function ($a, $b) {
return $b['score'] <=> $a['score'];
});
8.2、define定義常量數組
// version >= 5.6
const IT = ['sina', 'baidu', 'tencent'];
echo IT[0];
// version >= 7.0
define('IT', ['sina', 'baidu', 'tencent']);
echo IT[0];
參考:
《PHP7內核剖析》
手冊:PHP7新特性
總結
以上是生活随笔為你收集整理的php7与php5的区别,PHP7和PHP5区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java k线绘制,用Java绘制K线图
- 下一篇: php判断访客语言,php实现获取及设置