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

后端 未结 7 840
时光取名叫无心
时光取名叫无心 2020-12-17 15:12

I keep coming across statements like:

  • \"echo is a language construct but print is a function and hence has a return value\"
  • \"die is a language constr
相关标签:
7条回答
  • 2020-12-17 15:52

    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.

    0 讨论(0)
  • 2020-12-17 15:53

    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.

    0 讨论(0)
  • 2020-12-17 15:55

    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.

    0 讨论(0)
  • 2020-12-17 15:58

    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()".

    0 讨论(0)
  • 2020-12-17 15:59

    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.

    0 讨论(0)
  • 2020-12-17 16:00

    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.

    0 讨论(0)
提交回复
热议问题