php - Why can't you define a constant named EMPTY

后端 未结 4 939
无人及你
无人及你 2021-01-22 01:07

So... This is more of a curiosity, not a real problem. I don\'t get this:

Results in: Pars

相关标签:
4条回答
  • 2021-01-22 01:26

    empty is a reserved keyword in PHP. You cannot use any reserved words as constants, class names, function or method names as these words have special meaning in PHP. Use any other name as constant.

    0 讨论(0)
  • 2021-01-22 01:27

    Note that the reserved keywords are also case insensitive, and they can't be defined as constant or redefined using runkit_function_redefine();

    You can find here the exhaustive list of reserved keywords

    0 讨论(0)
  • 2021-01-22 01:31

    empty is not a function but a language construct (more like an operator than a function), which means it's resolved by the parser at parse time and not by the runtime. It's a reserved keyword and you cannot use it for anything yourself like function names, or constants.

    The reason empty cannot be a regular function is because it behaves differently. empty($undefined) would first resolve $undefined, throw an error, then pass null to empty; which is exactly the case empty is supposed to prevent, so it cannot play by the normal function invocation rules.

    0 讨论(0)
  • 2021-01-22 01:38

    You can definately define "EMPTY" as a constant. The problem is you can't use it directly.

    <?php 
     define("EMPTY", "valueForEmpty");
     echo EMPTY; // Wont work because empty is a keyword and PHP doesn't know that you mean the constant.
    
     echo constant("EMPTY"); // Works
    
    0 讨论(0)
提交回复
热议问题