PHP 代码中运算符前应该有空格吗?

发布于 2024-08-10 04:55:15 字数 339 浏览 1 评论 0原文

我最近看到很多代码的格式如下:

A:

if ($var=='test'){
    $var=Foo('blah'.$var1);
}else{
    // do something
}

我个人不喜欢它,更喜欢它:

B:

if ($var == 'test') {
    $var = Foo('blah' . $var1);
} else {
    // do something
}

我认为它更具可读性(注意添加空格)。

社区中是否存在普遍偏好,或者某种方式比另一种更好?

I've seen a lot of code recently formatted as the following:

A:

if ($var=='test'){
    $var=Foo('blah'.$var1);
}else{
    // do something
}

Personally I don't like it and would prefer it as:

B:

if ($var == 'test') {
    $var = Foo('blah' . $var1);
} else {
    // do something
}

I think it's much more readable (note the addition of spaces).

Is there a general preference in the community or is one way better than another?

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

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

发布评论

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

评论(5

感情废物 2024-08-17 04:55:15

最重要的是遵循标准并坚持下去。

也就是说,也许您可​​以遵循 Zend 的框架标准,并且它们使用空格。检查 C.4.6

if ($a != 2) {
    $a = 2;
}

希望有帮助!

The most important thing is to follow a standard and stick to it.

That said maybe you can follow Zend's Framework standards and they use spaces. Check C.4.6.

if ($a != 2) {
    $a = 2;
}

Hope it helps!

浅忆流年 2024-08-17 04:55:15

空格+1:)

但这只是我自己的标准,只要你保持一致并且代码清晰,就应该没问题

+1 for spaces :)

but that's just my own standard, as long as you are consistent and your code is clear, it should be okay

昔梦 2024-08-17 04:55:15

PHP 的语法与 C 非常相似。当我同时使用两者时,我倾向于遵循相同的风格。

例如,关键字与函数:

if ($foo) {

vs

MySuperDuperFunction($foo);

然后你遇到了缩进问题:

switch ($bar) {
    case CONSTANT:
        more_code();

.. 更容易阅读,因为

switch ($bar) {
case CONSTANT:
    more_code();

这表明 switch 和 case 处于同一级别,确实如此。它还有助于避免尚未优化的开关中出现疯狂的缩进。

您的风格应该说明您所使用语言的语法糖。这对于 PHP 和 C 来说都很奇怪,因为两者都有宽容的解析器。

如果你写这样的话:

if($a==2&&b==1&&c>3)
{

我会追捕你并要求你支付我的阿司匹林费用。这同样适用:

if (
    a==2
     &&
    b==1
     &&
    c>3
)
{

...看在上帝的份上,这不是 LISP!

PHP is much like C in its syntax. As I use both, I tend to follow the same style.

For instance, keywords vs functions:

if ($foo) {

vs

MySuperDuperFunction($foo);

Then you come to a question of indentation:

switch ($bar) {
    case CONSTANT:
        more_code();

.. is much easier to read as

switch ($bar) {
case CONSTANT:
    more_code();

This indicates that the switch and the case are on the same level, which they are. It also helps avoid crazy indentation in yet-to-be-optimal switches.

Your style should illustrate the syntactic sugar of the language you are using. This gets strange with PHP as well as C, because both have forgiving parsers.

If you write something like this:

if($a==2&&b==1&&c>3)
{

I'm going to hunt you down and demand that you pay for my aspirin. The same would go for this:

if (
    a==2
     &&
    b==1
     &&
    c>3
)
{

... For God sakes man, its not LISP!

怪我闹别瞎闹 2024-08-17 04:55:15

这是团队内部建立的惯例的问题。

最著名的约定是 Zend Framework 和 PEAR。您也可以创建自己的,只要确保它可读即可。

就我个人而言,我使用空格。

It's a matter of convetions that are stablished within your team.

The most famous conventions are Zend Framework and PEAR. You can also create your own, just make sure it is readible.

Personally, I use spaces.

左岸枫 2024-08-17 04:55:15

一般建议是标准化代码格式,使其符合一些最佳实践,并且广为人知,而不是发明一些自定义的东西。

PSR-2,编码风格指南将是最知名的方法,被许多人接受。 这里是事实,说明为什么保持代码格式是件好事。

The general advice would be to standardize the code formating, so it meets some best practices, and is widely known, instead of inventing something custom.

PSR-2, the coding style guide would be the best well known approach, accepted by many. here are facts, why it's good to keep the code formatting.

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