Why does PHP empty can access a non-existing index
Before we start, throw a question:
$arr = ; echo 'empty: ', PHP_EOL; var_dump(empty($arr['1'])); echo 'is_array: ', PHP_EOL; var_dump(is_array($arr['1']));
The result of running this code:
Have you ever had the same question as me? It is also a function, why
emptydoes it not report an error when accessing a non-existing index? It stands to reason that when a function is called, it will pass parameters by value, that is to say, two functions The first step of calling is to take
$arr['1']out this content, so why can one take out the other?
If you don’t understand, you have to ask, and then I searched variously on this question of passing parameters, but I didn’t find the answer I wanted. However, I feel that they are different. You have to ask me why, come:
Have you found that when the compiler displays, the color of the
is_arraythe function are different. But the
echois the same color, doesn’t it mean that they are the same kind of thing? Look again:
Did you find anything?
PHPThe keyword in this color is used. I seem to understand something.
Afterwards, I went through the official documentation, and I really found some clues. Under the
emptyfunction documentation, there is this paragraph:
Although I don’t know
语言构造器what it is,
函数I know it. And the official document also proposes that it
emptyis not a function, so it is obviously wrong to use it as a function for analysis above.
So now the question is, what is
语言构造器it? Simply put, it is the keywords defined by PHP. Although the usage looks like a function, it is directly mapped to a predefined series of operations when it is called, not like Parsing operations such as passing parameters and so on are performed in the same way as functions.
Let’s see another example to show the difference:
$isArrFun = 'is_array'; var_dump($isArrFun(2)); $empFun = 'empty'; var_dump($empFun(1));
When calling it for the second time, it reports an error: no
emptymethod. Does it seem to understand something, and further verify that it is not a function.
语言构造器is some predefined operations, the probability is more efficient than the function call. Therefore, it is
array_key_exists. When I just thought of this problem, I was still confused, since there is a more efficient way, then the
array_key_existsfunction exists What is the meaning of? After thinking about it, look at this paragraph:
$arr = [ 'a' => null, ]; var_dump(isset($arr['a'])); var_dump(array_key_exists('a', $arr));
When the element is
false. And the
If you want to ask me how to judge whether it is a function or a language constructor, the color displayed by the compiler is already a good reminder for you.
If the editor prompt you use is not perfect, then just pass the name to and
function_existshave a look. If it is not a function and can be called, then it is the latter.
I was troubled by this problem for a few days before, and today I made up my mind to study it. It’s okay, I found it… Just a brief look at it will solve the little confusion in my heart
empty. A small function, I didn’t expect it to be a keyword, disrespectful and disrespectful.
By the way, let me tell you quietly, in PHP, system functions can be redefined, as long as you write a function with the same name, but these guys don’t eat this set of keywords.