PHP 对变量名或函数名的长度有限制吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a limit in PHP to the length of a variable name or function name?
提问by james
If I wanted to write a really long variable name like:
如果我想写一个很长的变量名,如:
$this_is_my_variable_that_does_something_in_the_following_function_and_provides_some_information_with_which_the_function_relies_upon_to_do_all_the_work_it_needs = null;
would that work? same question for function/method names
那行得通吗?函数/方法名称的相同问题
回答by ceejayoz
Generally, such a limit is imposed by the threat of violence from other folks who interact with your code.
通常,这种限制是由与您的代码交互的其他人的暴力威胁施加的。
回答by Klaus Byskov Pedersen
From the documentation:
从文档:
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any numberof letters, numbers, or underscores
变量名遵循与 PHP 中其他标签相同的规则。有效的变量名以字母或下划线开头,后跟任意数量的字母、数字或下划线
The same is the case for function names, as stated here.
这同样适用于函数名的情况下,表示在这里。
回答by Nikola Svitlica
The limit in variable/function/method/class name length does not exist.
变量/函数/方法/类名长度的限制不存在。
Comments above states that that this property should not be exploited. That is true only when it comes to human readable/human maintainable code.
上面的评论指出不应利用此属性。只有当涉及到人类可读/人类可维护的代码时,这才是正确的。
However, this is extremely useful feature of PHP, that is exploited very well in a lot of very popular projects, such as Twig per example, which generates classes, example (a snippet):
然而,这是 PHP 非常有用的特性,在很多非常流行的项目中得到了很好的利用,比如 Twig per example,它生成类,example(一个片段):
class __TwigTemplate_9601167421099bbb502d119e674fed3fb71769eef256903648c9c2b4b6faa436 extends \Twig_Template {
protected function doDisplay(array $context, array $blocks = array())
{
$__internal_0abebc74dd811fd7b4cfa4c6a2fdf870d7723c04e8daf6926b04914d6644935f = $this->env->getExtension("native_profiler");
}
}
I had opportunity to benefit from same property as well in my projects.
我有机会在我的项目中也受益于相同的财产。
In conclusion:
综上所述:
- There is no limit in var/func/class name length
- It is extremely useful language feature
- There is a context for its usage, of course, it is not for every day work
- var/func/class 名称长度没有限制
- 这是非常有用的语言功能
- 它的用法是有上下文的,当然,它不是用于日常工作
回答by zsalzbank
There is no limit - but it is highly not suggested as it creates unreadable code...
没有限制 - 但强烈不建议这样做,因为它会创建不可读的代码......
回答by Craige
PHP does not pose a length limit on it's identifiers.
PHP 对其标识符没有长度限制。
That said, I'm not sure why anybody would ever want to create a 160 character variable name. I hope this is a hypothetical question.
也就是说,我不确定为什么有人会想要创建一个 160 个字符的变量名称。我希望这是一个假设性的问题。
回答by Rupesh Ranjan
PHP doesn't have any restriction for variable name. And my suggestion is that variable name must consist the definition of the data which you are gonna store into it.
PHP 对变量名没有任何限制。我的建议是变量名必须包含您要存储到其中的数据的定义。