PHP variable / function / class names using special characters

后端 未结 5 1529
忘了有多久
忘了有多久 2021-02-12 22:40

I understand that the underscore _ is an acceptable character for naming variables / functions / classes etc. However I was wondering if there are any other special

相关标签:
5条回答
  • 2021-02-12 22:56

    Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

    http://www.php.net/manual/en/functions.user-defined.php

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

    http://www.php.net/manual/en/language.variables.basics.php

    See also, the Userland Naming Guide: http://www.php.net/manual/en/userlandnaming.php

    0 讨论(0)
  • 2021-02-12 22:56

    A valid {variable | function} name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

    http://www.php.net/manual/en/functions.user-defined.php http://www.php.net/manual/en/language.variables.basics.php

    0 讨论(0)
  • 2021-02-12 23:15

    If you check the docs on variables it says that:

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

    But basically people have agreed to only use a-zA-Z0-9_ and not the "fancy" names since they might break depending in the encoding one uses.

    So you can have a variable that is named $aöäüÖÄ but if you save that with the wrong encoding you might run into trouble.


    The same goes for functions too btw.

    So

    function fooööö($aà) { echo $aà; }
    
    fooööö("hi"); // will just echo 'hi'
    

    will just work out (at least at first).


    Also check out:

    Exotic names for methods, constants, variables and fields - Bug or Feature?

    for some discussion on the subject.

    0 讨论(0)
  • 2021-02-12 23:19

    If you want to structure names by combining elements (for example functions, and database column or tables names), you could use a double underscore as a separator:

    social_security_number__check(),
    musical_instrument__tune(),
    vehicle__insert()
    

    or

    check__social_security_number(),
    tune__musical_instrument(),
    insert__vehicle()
    
    0 讨论(0)
  • 2021-02-12 23:23

    You can find the information you're looking for the PHP Manual. Know, has been answered:

    Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

    Function as regular expression: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

    The information in the manual does not reflect Namespaces (some might see them as part of the function name). And with some identifiers the information in the PHP manual is not precise. See PHP Syntax Regulary Expressed for various elements and the links into the PHP Manual for them.

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