PHP变量/函数/类名称使用特殊字符 [英] PHP variable / function / class names using special characters

查看:162
本文介绍了PHP变量/函数/类名称使用特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,下划线 _ 是命名变量/函数/类等的可接受字符。然而,我想知道是否还有其他可以使用的特殊字符。我测试了几个没有运气,并假设很长一段时间没有,但我想我会看看是否有其他人知道一定。这主要是出于美学目的,但我想象一个特殊的字符命名约定在与其他开发人员一起定义值类型等时非常有用。

div>

如果您检查 文档它表示:


变量名称遵循与PHP中其他标签相同的规则。有效的变量名称以字母或下划线开头,后跟任意数量的字母,数字或下划线。作为正则表达式,它会这样表示:'[a-zA-Z_\x7f-\xff] [a-zA-Z0-9_\x7f-\xff] *'


但基本上人们已经同意只使用 a-zA-Z0-9 _ 而不是花式名称,因为它们可能会因为使用的编码而中断。



因此可以有一个名为 $aöäüÖÄ ,但如果您使用错误的编码保存,您可能会遇到问题




函数也是如此btw。

所以

  functionfooööö($aà){echo $aà; } 

fooööö(hi); / /只会回声'嗨'

只会起作用(至少在第一时间)请注意:

//sackoverflow.com/questions/3417180/exotic-names-for-methods-constants-variables-and-fields-bug-or-feature\">方法,常量,变量的异常名称和字段 - 错误或功能?


对这个主题进行了一些讨论。


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 characters which can be used. I tested out a few with no luck and have assumed for a long time that there are not, however I figured I would see if anyone else knows for certain. This would be mostly for aesthetic purposes, however I imagine a special character naming convention would be useful when working with other developers to define value types etc.

解决方案

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.

这篇关于PHP变量/函数/类名称使用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆