PHP: What are language constructs and why do we need them?
Asked Answered
P

7

21

I keep coming across statements like:

echo is a language construct but print is a function and hence has a return value

and

die is a language construct

My question is what are these language constructs and more importantly why do we need them?

Phantom answered 15/7, 2010 at 9:41 Comment(1)
print is also a language construct. It has a value, but it’s not actually a return value. The value makes it possible to combine print with other instructions in the same statement. You can recognise print as a language construct in that it doesn’t require parentheses.Impi
T
29

Language constructs are hard coded into the PHP language. They do not play by normal rules.

For example, whenever you try to access a variable that doesn't exist, you'd get an error. To test whether a variable exists before you access it, you need to consult isset or empty:

if (isset($foo))

If isset was a normal function, you'd get a warning there as well, since you're accessing $foo to pass it into the function isset. Since isset is a language construct though, this works without throwing a warning. That's why the documentation makes a clear distinction between normal functions and language constructs.

Tallia answered 15/7, 2010 at 9:48 Comment(0)
I
12

Language constructs are what makes up the language: things like "if" "for" "while" "function" and so on.

The mentions in the PHP manual of things like "echo", "die" or "return" are there to make it clear that these are NOT functions and that they do not always behave like functions.

You could call "echo" as "echo()" so it may confuse beginners. That's why they put the clear disinction in the manual. To make it absolutely clear to everyone.

Other examples for language constructs that could be mistaken for functions are "array()", "list()" and "each()".

Iorio answered 15/7, 2010 at 9:45 Comment(1)
Unfortunately, the syntax description of language constructs in the PHP manual is also confusing. Just take a look at the one for echo: it says void echo ( string $arg1 [, string $... ] ) but echo ($arg1, $arg2); is invalid.Weixel
E
6

To understand the answer for this question you must understand how parsers work. A language is defined by syntax and the syntax is defined through keywords.

The language constructs are pieces of code that make the base of PHP language. The parser deals with them directly instead of functions.

Eggcup answered 15/7, 2010 at 10:4 Comment(0)
S
4

Not all of a language can be functions. There must be some base, somewhere, on which you implement those first functions. The elements of this base are the language constructs (alternately, built-ins). They don't always behave like "normal" functions do.

Shorttempered answered 15/7, 2010 at 9:46 Comment(0)
I
3

For the sake of completeness, a language construct is any instruction which is built into the language itself, while a function is an additional block of code.

In some cases, a language may choose to build in a particular feature or to rely on a separate function.

For example, PHP has the print language construct, which outputs a string. Many other languages, such as C don’t build it in, but implement it as a function. There might be technical reasons for taking one or other approach, but sometimes it is more philosophical — whether the feature should be regarded as core or additional.

For practical purposes, while functions follow a rigid set of logistic rules, language constructs don’t. Sometimes, that’s because they may be doing something which would otherwise traumatise a regular function. For example, isset(…), by its very purpose, may be referencing something which doesn’t exist. Functions don’t handle that at all well.

Here are some of the characteristics of language constructs:

  • Many don’t require parentheses; some do sometimes.
  • Language Constructs are processed in a different stage; functions are processed later
  • Some Language Constructs, such as isset do things which would be impossible as functions; some others, such as Array(…) could have gone either way.
  • Some Language Constructs certainly don’t look like functions. For example, the Array(…) construct can be written as […].
  • As the documentation keeps reminding us, language constructs cannot be referenced as variable variables. So $a='print_r'; $a(…); is OK, but $a='print'; $a(…); isn’t.
Impi answered 3/3, 2019 at 8:0 Comment(0)
P
2

Some things are just not possible using normal functions, consider this snippet:

list($el1, $el2) = array('el1', 'el2');

What it does is it takes the elements from a non-associative array and assigns the values to the variables defined in the list() construct.

Simply cannot be done with functions :)

A more subtle example is issetand empty. Though they look like functions, they one thing that's not possible with functions alone – they do not generate "variable is undefined" or "undefined index" notices.

Peria answered 15/7, 2010 at 9:44 Comment(1)
You can re-implement empty with isset: function empty(&$var) { return !isset($var) || !$var; }Weixel
H
2

language constructs can be formed in more than one way and has a return-value

print("asdf"); is as possible as print "asdf"; and will return 1.

echo("asdf"); is equal to echo "asdf;" but has no return-value.

die("asdf"); is equal to exit("asdf"); and hasn't a return-value too.

Hispanicism answered 15/7, 2010 at 9:48 Comment(1)
Actually, die and exit do have a value, but you never get to see it. This is why you can use them in a statement such as doSomething() or die('oops');. The or operator needs to be between values.Impi

© 2022 - 2024 — McMap. All rights reserved.