search
Homephp教程PHP源码PHP 什么是语言构造器 什么是可变函数

天偶然间发现水煮鱼中的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是这么写的:


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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.