为什么 PHP 中的函数和方法不区分大小写?

2021-12-29 00:00:00 syntax php case-insensitive

PHP 中的函数和方法不区分大小写,如下例所示.

Functions and methods in PHP are case-insensitive as illustrated in the following example.

function ag()
{
    echo '2';
}

Ag();
class test {
    function clMe()
    {
        echo 'hi';
    }
}

$instance = new test;
$instance->clme();

但这不是变量的情况.理由是什么?

But that's the not case with variables. What's the rationale?

推荐答案

让我引用 采访——PHP 的创造者 Rasmus Lerdorf

PHP 的第一个版本是一组简单的工具,我为我的网站和几个项目组合了这些工具.一个工具对 mSQL 数据库进行了一些奇特的命中记录,另一个充当表单数据解释器.在我厌烦之前,我最终得到了大约 30 个用 C 编写的不同的小 CGI 程序,并将它们组合成一个 C 库.然后我编写了一个非常简单的解析器,它会从 HTML 文件中挑选标签,并将它们替换为 C 库中相应函数的输出.

The first version of PHP was a simple set of tools that I put together for my Website and for a couple of projects. One tool did some fancy hit logging to an mSQL database, another acted as a form data interpreter. I ended up with about 30 different little CGI programs written in C before I got sick of it, and combined all of them into a single C library. I then wrote a very simple parser that would pick tags out of HTML files and replace them with the output of the corresponding functions in the C library.

这个简单的解析器慢慢地成长为包含条件标签,然后是循环标签、函数等.我从来不认为我在写一种脚本语言.我只是向宏替换解析器添加了一点功能.我仍在用 C 编写所有真正的业务逻辑.

The simple parser slowly grew to include conditional tags, then loop tags, functions, etc. At no point did I think I was writing a scripting language. I was simply adding a little bit of functionality to the macro replacement parser. I was still writing all my real business logic in C.

我在某处读到过,因为引入的所有函数本质上都像 HTML 文档中的标签,并且由于 HTML 标签不区分大小写,所以他在 PHP 中选择了不区分大小写的函数名称.此功能后来保留在语言中.

I have read somewhere that since all the functions introduced essentially felt like tags in an HTML document and since HTML tags were case insensitive, he chose function names in PHP to be case insensitive. Later on this feature remained on in the language.

相关文章