In the PHP Manual we can find statements like this:
list()
is used to assign a list of variables in a single operation. Likearray()
, it's not really a function, it's a constructor of the language .
It always strikes me when the Manual emphasizes that difference . I imagine that a constructor of the language would be a kind of internal function, while a function would be the ones created by the programmer, but it is not entirely clear to me.
I would appreciate any answer that clears my doubts.
In short: the language constructor is the Lego pieces you have. Functions are the combinations of pieces with which you implement functionalities.
Language constructs are the fundamental words that make up a programming language. In PHP these would be
if
,while
... They are hardcoded in the language and have special rules. Functions are created from these constructors.Put another way, in the same way that PHP code must be broken down into lower-level language for the server to understand it, the PHP parser breaks functions down into language constructors before parsing them.
One of the consequences is that their invocation is faster than predefined or user-defined functions. Why? Well, because the functions are interpreted from their dissection in all the constructors that they form.
For example, when you try to access a variable that doesn't exist, you get an error. To validate if a variable exists before accessing it, you need to query
isset
orempty
:If
isset()
it were a normal function, it would give you a warning since you are accessing $foo to pass it to the functionisset()
. Since itisset()
's a language constructor, however, this works without throwing that warning. That's why the documentation makes a clear distinction between normal functions and language constructors.The list of language constructors is given in List of PHP web reserved words :