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

后端 未结 7 871
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题