Difference Between a Language Construct and a "Built-In" Function in PHP

What is the difference between a language construct and a built-in function in PHP?

(This is longer than I intended; please bear with me.)

Most languages are made up of something called a "syntax": the language is comprised of several well-defined keywords, and the complete range of expressions that you can construct in that language is built up from that syntax.

For example, let's say you have a simple four-function arithmetic "language" that only takes single-digit integers as input and completely ignores order of operations (I told you it was a simple language). That language could be defined by the syntax:

// The | means "or" and the := represents definition
$expression := $number | $expression $operator $expression
$number := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
$operator := + | - | * | /

From these three rules, you can build any number of single-digit-input arithmetic expressions. You can then write a parser for this syntax that breaks down any valid input into its component types ($expression, $number, or $operator) and deals with the result. For example, the expression 3 + 4 * 5 can be broken down as follows:

// Parentheses used for ease of explanation; they have no true syntactical meaning
$expression = 3 + 4 * 5
= $expression $operator (4 * 5) // Expand into $exp $op $exp
= $number $operator $expression // Rewrite: $exp -> $num
= $number $operator $expression $operator $expression // Expand again
= $number $operator $number $operator $number // Rewrite again

Now we have a fully parsed syntax, in our defined language, for the original expression. Once we have this, we can go through and write a parser to find the results of all the combinations of $number $operator $number, and spit out a result when we only have one $number left.

Take note that there are no $expression constructs left in the final parsed version of our original expression. That's because $expression can always be reduced to a combination of other things in our language.

PHP is much the same: language constructs are recognized as the equivalent of our $number or $operator. They cannot be reduced into other language constructs; instead, they're the base units from which the language is built up. The key difference between functions and language constructs is this: the parser deals directly with language constructs. It simplifies functions into language constructs.

The reason that language constructs may or may not require parentheses and the reason some have return values while others don't depends entirely on the specific technical details of the PHP parser implementation. I'm not that well-versed in how the parser works, so I can't address these questions specifically, but imagine for a second a language that starts with this:

$expression := ($expression) | ...

Effectively, this language is free to take any expressions it finds and get rid of the surrounding parentheses. PHP (and here I'm employing pure guesswork) may employ something similar for its language constructs: print("Hello") might get reduced down to print "Hello" before it's parsed, or vice-versa (language definitions can add parentheses as well as get rid of them).

This is the root of why you can't redefine language constructs like echo or print: they're effectively hardcoded into the parser, whereas functions are mapped to a set of language constructs and the parser allows you to change that mapping at compile- or runtime to substitute your own set of language constructs or expressions.

At the end of the day, the internal difference between constructs and expressions is this: language constructs are understood and dealt with by the parser. Built-in functions, while provided by the language, are mapped and simplified to a set of language constructs before parsing.

More info:

  • Backus-Naur form, the syntax used to define formal languages (yacc uses this form)

Edit: Reading through some of the other answers, people make good points. Among them:

  • A language builtin is faster to call than a function. This is true, if only marginally, because the PHP interpreter doesn't need to map that function to its language-builtin equivalents before parsing. On a modern machine, though, the difference is fairly negligible.
  • A language builtin bypasses error-checking. This may or may not be true, depending on the PHP internal implementation for each builtin. It is certainly true that more often than not, functions will have more advanced error-checking and other functionality that builtins don't.
  • Language constructs can't be used as function callbacks. This is true, because a construct is not a function. They're separate entities. When you code a builtin, you're not coding a function that takes arguments - the syntax of the builtin is handled directly by the parser, and is recognized as a builtin, rather than a function. (This may be easier to understand if you consider languages with first-class functions: effectively, you can pass functions around as objects. You can't do that with builtins.)

What is the meaning of language construct can not be called through variable function?

echo() and isset() (just to pick those exemplary for other PHP language constructs) can't be called in a variable function.

Here comes an example of a variable function.

function foo() {
echo "foo";
}

$func1 = 'foo';
$func1(); // "foo" will be output

And now let's try with echo:

$func2 = 'echo';
$func2(); // "Fatal error: Call to undefined function echo() on line 10"

That's because echo() isn't a function, but a language construct.

What does 'Language Construct' mean?

First, you need to understand what a constructed language Formal Language is. All programming languages are constructed formal languages (read the reference). You may then read a little bit about compiler construction, including this reference as well.

Going back to your question, consider this: The English language (a natural language) has tokens 'A-Z/0-9/,;"...' which we use to build "words" and we use languages rules to build sentences out of words. So, in the English language, a construct is what we build out of tokens.

Consider this brick-and-mortar example: Imagine if you set out to build a house, the basic materials you might use are: sand, iron, wood, cement, water (just five for simplicity). Anything you build out of these 4 or 5+ items would be a "construct", which in turn helps you build your house.

I have intentionally omitted details to further simplify the answer; hope this is helpful.

PHP: What's wrong with this line of code?

unset() is a language construct that requires parentheses; you must use unset($player[$x]);.

What and which are the PHP functions that are not callable?

In a language or framework, you can usually search for limitations like this under the term of "reserved words".

In PHP's case, you can find a list of reserved keywords here:
https://www.php.net/manual/en/reserved.keywords.php

PHP hidden trick or a supported feature?

It's documented in the entry for echo:

void echo ( string $arg1 [, string $... ] )

The two forms are not actually equivalent, since there's a difference in the instant in which functions are evaluated.



Related Topics



Leave a reply



Submit