在 php 中声明变量的最佳标准

发布于 2024-09-01 01:31:07 字数 176 浏览 4 评论 0原文

任何人都可以让我知道 php 变量的全局声明..我在网站中看到过这样的一个..

for integer start with i, eg: $iItemId
for array start with a, eg: $aItemIds
for string start with s, eg: $sItemName

can any one please let me know the global declaration of php variables..i have seen one among the site like..

for integer start with i, eg: $iItemId
for array start with a, eg: $aItemIds
for string start with s, eg: $sItemName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惟欲睡 2024-09-08 01:31:07

使用您觉得舒服的风格。

但还要考虑您正在使用的框架。如果您主要使用 Zend Framework,选择与其风格不同的风格可能会导致“视觉上混乱”的代码,因为风格会切换。

Use the style you are comfortable with.

But also take into account the frameworks you are using. If you use mostly the Zend Framework choosing a style different to their style may lead to "visually confusing" code, since the style switches.

我的奇迹 2024-09-08 01:31:07

我不使用这样的约定,而且在 php 中很少看到这种情况,我猜这更像是 VB 的事情......

如果你给变量起个好名字,它们的类型就会出现。例如,我使用变量命名的不太严格的约定:

  • $i, $j, $k, $l< /code> 等,所有单字母变量都是用作计数器的整数
  • $messageRow =>它是一个数据库结果实例
  • $messageCollection =>它是结果集合
  • $messageRows => array ,使用复数表示多个项目
  • $messageTitle =>它是一个字符串
  • $messageVisible$isMessageVisible =>它是一个布尔值

事实上,除了数组和字符串之外,现在一切都趋向于对象,并且具有良好注释和/或可见类型转换(主要是在方法参数或特定用途上)的良好命名使变量的代码更具可读性,并且猜测类型比使用更容易一大堆 $oThis$oThat

I do not use such conventions, and it is rare to see this in php, it's more a VB thing I guess...

If you give good names to variable, their types will appear. For instance, the not so strict conventions I use vor variable naming :

  • $i, $j, $k, $l, etc. and all single letters variables are integers used as counters
  • $messageRow => it's a DB result instance
  • $messageCollection => it's results collection
  • $messageRows => array , Using plural denotes multiple items
  • $messageTitle => it's a string
  • $messageVisible or $isMessageVisible => it's a boolean

In fact everithing now tends to be object, except array and strings, and good naming with good comment and/or visible typecast (mainly on methods arguments, or specific use) makes variables more readable code and guessing types is easier than having a whole bunch of $oThis and $oThat

巾帼英雄 2024-09-08 01:31:07

您所描述的称为 匈牙利表示法,似乎还没有人提到过。

在回答您的问题时,我建议使用您最喜欢的任何风格。只要它是一致的和可读的,这并不重要。也就是说,匈牙利表示法并不是最具可读性的风格......

What you're describing is called Hungarian Notation, nobody seems to have mentioned that yet.

In answer to your question, I would suggest using whatever style you're most comfortable with. As long as it's consistent and readable it doesn't really matter. That said, hungarian notation isn't the most readable style...

后eg是否自 2024-09-08 01:31:07

天啊,不!

尽量避免使用愚蠢的名字 LikeThisOne。

永远不要在变量名中使用大写字母,除非是全部大写。

如果需要,可以使用下划线分隔单词。

God, no!

Avoid like hell idiotic names LikeThisOne.

Never ever use caps in a variable name, unless it's ALLCAPS.

Use underscore to separate words if you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文