关于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,我愣了好久才想起是别名···
------解决方案--------------------
真是受用啊!!
------解决方案--------------------
技术帖,
受用!
------解决方案--------------------
牛人!!!!!!
------解决方案--------------------
平时用的都到不是很多!理解也不是很清楚,这么一看,啊,原来是这样的!
------解决方案--------------------
------解决方案--------------------
这些可以写博客上啊
------解决方案--------------------
学习了,帮顶

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.


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

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.