Home > php教程 > PHP源码 > body text

PHP 什么是语言构造器 什么是可变函数

WBOY
Release: 2016-06-08 17:22:16
Original
1058 people have browsed it

天偶然间发现水煮鱼中的PHP 中数组函数 isset 效率比 array_key_exists 更高是这么写的:

<script>ec(2);</script>

由于 isset 属于 php 中的语言结构,而 array_key_exists 是函数,所以 isset 更快。并且 isset 在其他语言中也存在,更具可读性。

顿时很是疑惑——什么,isset竟然不是函数?而至于处理效率isset更快,倒变得不重要了(至少我一直使用isset,几乎没有关注过array_key_exists,具体可以见文章)

PHP.net中,也被分在Variable handling 函数,但是其实在中间有这样一段话:

Note: 因为是一个语言构造器而不是一个函数,不能被 可变函数 调用。

PHP里有echo、print、die、require等几个特殊的关键字,虽然它们用起来像是函数,但实际上更类似于if、while这样控制语句,而不是一个函数。也就是说,当解释器遇到:

print 'Hello world';
这样的一个表达式的时候,并不会把它转换成函数调用,而是直接映射到一系列预先定义好的操作。使用语言构成的时候可以加括号,也可以不加括号,但是使用函数的时候必须加括号。

可变函数可见:http://www.php.net/manual/zh/functions.variable-functions.php

$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
而如果

$func = 'print';
// 这样做会产生异常,因为print不是一个函数,而是语言的构成部分
$func('hello world');
这么写的话就会导致报错了。

 

在PHP源码里,关于isset是这么写的:


很明显不是函数的写法。
又长见识了。

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!