关于php中的“别号”
关于php中的“别名”
今天有个朋友贴了百度知道一个询问exit和die区别的问题。
里面的标准答案大概意思是:
两个有区别,die是退出并释放内存,exit是退出但不释放内存。
这个解释显然是错的,我们以前都看过手册中说,两者只是别名关系,除此之外完全一样。
不过我还是很好奇,决定从源码中找找线索,看看php是如何处理的这个“别名”。
首先要清楚一点,die和exit都是语言结构而非函数。很多初学者总搞不清语言结构和函数的区别,用通俗点的话讲,语言结构可以理解为语法本身的一种标识。像+、-、*、/这些也都是语言结构,if、else、for、while,这些都是语言结构。是语法本身的一部分。任何语言都会有这些东西,因为计算机看到+不会认为是应该做加法的。这需要编译器转换为机器码也就是cpu能够识别的指令集。
php执行源码时的整个过程为,首先按照zend_language_scanner.l中定义的,将源码中的echo、if之类的语言结构转换成类似的T_ECHO、T_IF这些token,并且会去掉源码中的空格,注释这些与程序逻辑无关的字符。,就形成了一些简短的表达式,这就是词法分析阶段。然后会按照zend_vm_opcodes.h中定义的,将这些token转换为op code。然后一条一行的执行这些op code。
上面大概解释了php的编译和执行的过程,以及语言结构的定义。下面进入正题。
我们也应该记得,php中有很多别名函数,比如:implode和join。无论是别名函数还是别名语言结构,从实际效果角度讲,都是一样的,不过源码的处理方式肯定还是不一样的。
我们先看看这个别名语言结构是如何处理的,稍后再看别名函数。
zend_language_parser.c中,定义了一个宏
#define T_EXIT 300
还定义了一个enum,里面也有
enum yytokentype {
...
T_EXIT = 300,
....
}
这里告诉我们,T_EXIT这个token,它的code是300。
再看zend_language_scanner.l,其中有这么几行代码。
return T_EXIT;
}
return T_EXIT;
}
很明显,php做词法分析时,无论遇到exit还是die,都会返回T_EXIT这个token。从这里酒可以证明,die和exit,再php内部处理是完全一样的。
也可以用下列php代码来确定:
var_dump(token_get_all(""));
返回的结果中die和exit对应的token code,都是300。
关于die和exit的问题,我们已经可以确定了。在这里,再引申出一个问题。也是我一直忽略的一个细节:
&&、||与AND、OR一样吗?
我先坦白,之前我一直以为一样,以为是纯粹的别名关系。但今天看到源码后,发现完全是不同的token。拿&&和AND举例:
还是zend_language_scanner.l
return T_BOOLEAN_AND;
}
return T_LOGICAL_AND;
}
一个叫布尔"与",一个叫逻辑"与"
之所以使用不同的token。那必然有不同之处。这里我也不卖关子了,google能找到很多答案,其实这两个最实质的区别就是优先级不同:
$a = 1 && 0;
$b = 1 AND 0;
var_dump($a);
var_dump($b);
前者会尝试先计算1 && 0,得到结果后再赋给$a,后者会先将1赋给$b;所以结果为
bool(false) int(1)
这下大家应该清楚这里的细节了。用的时候需要注意下。
刚才说的都是语言结构的“别名”,那么php中的函数别名是如何处理的呢?
拿implode和join举例:
basic_function.c中,可以找到如下一行:
PHP_FALIAS(join, implode, arginfo_implode)
那么很明显了,是PHP_FAILIAS这个宏起的作用。下面还能找到许多,比如ini_set和ini_alter也是别名关系。想深究下去,就一路去追这个宏吧。
php.h中
#define PHP_FALIAS ZEND_FALIAS
发现PHP_FALIAS又指向ZEND_FALIAS
zend_API.h中
#define ZEND_FALIAS(name, alias, arg_info) ZEND_FENTRY(name, ZEND_FN(alias), arg_info, 0)
...
#define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },
再往下就是函数初始化之类的工作了,我们也知道别名函数也知道大概是怎么回事了。
------解决方案--------------------
学习了,一般习惯用exit;
------解决方案--------------------
------解决方案--------------------
涨见识了
------解决方案--------------------
学习
今天看一个app上的笑话
也是讲的php exit与die
举的是生孩子的例子.............................
------解决方案--------------------
首先,别名这个问题长见识了。
另外 && 和 and 我只知道,当使用 条件1 && 条件2 的时候,如果条件1为false,那么条件2就不会执行,and则会计算2个,今天算是知道本质原因了。
------解决方案--------------------
学习了......
------解决方案--------------------
看来我是一直有误解啊,多谢仁兄的耐心解释。
------解决方案--------------------
xie xie
谢 谢
------解决方案--------------------
第一次见token_get_all()这个函数
------解决方案--------------------
学知识
来了
------解决方案--------------------
学习了...
------解决方案--------------------
太感谢楼主了!像这样的技术帖子确实不常见到。
------解决方案--------------------
我一直用die,有天我问我同学用过没,他说他exit,我愣了好久才想起是别名···
------解决方案--------------------
真是受用啊!!
------解决方案--------------------
技术帖,
受用!
------解决方案--------------------
牛人!!!!!!
------解决方案--------------------
平时用的都到不是很多!理解也不是很清楚,这么一看,啊,原来是这样的!
------解决方案--------------------
------解决方案--------------------
这些可以写博客上啊
------解决方案--------------------
学习了,帮顶

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
