与 echo 和 return 连接时句号和逗号之间的区别?

发布于 2024-08-05 19:01:03 字数 340 浏览 1 评论 0原文

我刚刚发现这会起作用:

echo $value , " continue";

但这不起作用:

return $value , " continue";

虽然 . 可以代替 ,echoreturn< /代码> 语句。

这里的句号和逗号有什么区别?

I just found that this will work:

echo $value , " continue";

but this does not:

return $value , " continue";

While . works instead of , in both the echo and return statements.

What is the difference between a period and a comma here?

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

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

发布评论

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

评论(9

return 只允许一个表达式,但 echo 允许表达式列表,其中每个表达式以逗号分隔。

但请注意,由于 echo 不是一个函数,而是一种特殊的语言结构,因此将表达式列表括在括号中是非法的。

return only allows one expression, but echo allows a list of expressions where each expression is separated by a comma.

But note that since echo is not a function but a special language construct, wrapping the expression list in parenthesis is illegal.

苍风燃霜 2024-08-12 19:01:03

您还必须注意,echo 作为一种构造,使用逗号比使用点更快

因此,如果您加入某个角色 400 万次,您将得到以下结果:

echo $str1, $str2, $str3;

大约2.08

echo $str1 。 $str2 。 $str3;

大约3.48

几乎需要您在上面看到的一半时间。

这是因为带有点的 PHP 会首先连接字符串,然后输出它们,而带有逗号的 PHP 只是将它们一个接一个地打印出来。

我们谈论的是几分之一秒,但仍然如此。

原始来源

You also have to note that echo as a construct is faster with commas than it is with dots.

So if you join a character 4 million times this is what you get:

echo $str1, $str2, $str3;

About 2.08 seconds

echo $str1 . $str2 . $str3;

About 3.48 seconds

It almost takes half the time as you can see above.

This is because PHP with dots joins the string first and then outputs them, while with commas just prints them out one after the other.

We are talking about fractions of a second, but still.

Original Source

但可醉心 2024-08-12 19:01:03

. 是 PHP 中的连接运算符,用于将两个字符串连接在一起。

逗号可用于 echo 的多个输入。

The . is the concatenation operator in PHP, for putting two strings together.

The comma can be used for multiple inputs to echo.

野稚 2024-08-12 19:01:03

点 (.) 用于连接变量或字符串。这就是为什么当您在连接两个字符串时进行回显时它会起作用,并且当您在方法中返回字符串的串联时它会起作用。但是逗号没有连接,这就是 return 语句不起作用的原因。

echo 是一种可以采用多个表达式的语言构造,这就是逗号起作用的原因:

void echo ( string $arg1  [, string $...  ] )

使用点进行连接。

Dot (.) is for concatenation of a variable or string. This is why it works when you echo while concatenating two strings, and it works when you return a concatenation of a string in a method. But the comma doesn't concatenate and this is why the return statement won't work.

echo is a language construct that can take multiple expressions which is why the comma works:

void echo ( string $arg1  [, string $...  ] )

Use the dot for concatenation.

故事未完 2024-08-12 19:01:03

echo 是一种语言构造(不是函数),可以接受多个参数,这就是 , 起作用的原因。使用逗号会稍微均匀(但只有几纳秒,无需担心)

. 是字符串的连接运算符(粘合剂)

echo is a language construct (not a function) and can take multiple arguments, that's why , works. using comma will be slightly even (but only some nanoseconds, nothing to worry about)

. is the concatenation operator (the glue) for strings

耀眼的星火 2024-08-12 19:01:03

echo 实际上是一个函数(不是真的,但我们可以说它是为了为了论证),它接受任意数量的参数并将它们连接在一起。

虽然 return 不是一个函数,而是一个关键字,它告诉函数返回值,并且它试图将 , 解释为某种运算符。当您使用 return 语句时,您应该使用 . 作为串联运算符。

echo is actually a function (not really, but let's say it is for the sake of argument) that takes any number of parameters and will concatenate them together.

While return is not a function, but rather a keyword, that tells the function to return the value, and it is trying to interpret , as some kind of operator. You should be using . as the concatenation operator in the case when you are using the return statement.

剧终人散尽 2024-08-12 19:01:03

值得一提的是,连接运算符 . 比许多其他运算符具有更高的优先级,并且与 +- 运算符具有相同的优先级

为什么这很重要?

好吧,空谈是廉价的,让我向您展示代码(来自 PHP 文档

$x = 4;
// this line might result in unexpected output:
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// because it is evaluated like this line:
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// the desired precedence can be enforced by using parentheses:
echo "x minus one equals " . ($x-1) . ", or so I hope\n";

事实上,从 PHP 7.4.0 开始,第一行将发出弃用消息

已弃用:包含以下内容的无括号表达式的行为
两个都 '。'和 '+'/'-' 将在 PHP 8 中发生变化:'+'/'-' 将采取更高的
优先级

因此,在 PHP 8 中,这种情况下的关联性问题似乎可以通过给予 +- 运算符更高的优先级来解决。

那么现在我们可以说 ., 在使用 echo 时会给出相同的结果吗?

不,它们不会总是给出相同的结果结果

让我们以这种情况为例,

echo ' Here\'s ' . $name ?? 'Johnny';

这里我们使用了 空合并运算符 因此,如果 $name 存在且不为 NULL,它将被返回,否则将返回 Johnny。乍一看,人们可能会认为结果将是 Here's Johnny 因为 $name 未定义,或者他们希望如此。

实际上结果将是

PHP Notice:  Undefined variable: name
Here's 

这里发生的是 ?? 运算符的优先级低于 . 这意味着 PHP 将尝试计算 (Here's $name)< /em> 首先。

您可以通过将表达式括在括号中

echo ' Here\'s ' . ($name ?? 'Johnny');

或仅使用逗号来解决此问题。

echo ' Here\'s ' , $name ?? 'Johnny';

It's worth mentioning that the concatenation operator . has a higher precedence than lots of other operators and has equal precedence with + and - operators

Why is this important?

Well, talk is cheap let me show you the code ( from PHP documentation)

$x = 4;
// this line might result in unexpected output:
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// because it is evaluated like this line:
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// the desired precedence can be enforced by using parentheses:
echo "x minus one equals " . ($x-1) . ", or so I hope\n";

In fact, the first line will issue a deprecation message as of PHP 7.4.0

Deprecated: The behavior of unparenthesized expressions containing
both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher
precedence

So in PHP 8 it seems the problem of associativity in this case will be solved by giving + and - operators a higher precedence.

So can we say now that . and , when using echo give the same result?

No, they will not always give the same result

Let's take this case for example

echo ' Here\'s ' . $name ?? 'Johnny';

Here we used the Null coalescing operator so if $name exists and is not NULL it'll be returned otherwise it returns Johnny. At first glance, one may think the result will be Here's Johnny since $name is not defined or so they hope.

Actually the result will be

PHP Notice:  Undefined variable: name
Here's 

What happened here is that ?? operator has a lower precedence than the . which means PHP will try to evaluate (Here's $name) first.

You can solve this by either enclosing the expression in parentheses

echo ' Here\'s ' . ($name ?? 'Johnny');

Or simply use a comma.

echo ' Here\'s ' , $name ?? 'Johnny';
花开半夏魅人心 2024-08-12 19:01:03

与 Mr.Web 的答案相比,2022 年的 PHP 的速度更快。

$ cat test.php 
<?php

$iterations = 10000;
$file_out ="./benchmark";
$precision = 8;

function dot()
{
        echo "Hello" . "World\n";
}

function comma()
{
        echo "Hello" , "World\n";
}

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        dot();
}
$end = hrtime(true);
$out = "   dot: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        comma();
}
$end = hrtime(true);
$out = " comma: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$ php test.php
...snip...

$ cat benchmark 
   dot: 22893557
 comma: 29056406

In contrast with Mr.Web's answer PHP in 2022 is faster with the dots.

$ cat test.php 
<?php

$iterations = 10000;
$file_out ="./benchmark";
$precision = 8;

function dot()
{
        echo "Hello" . "World\n";
}

function comma()
{
        echo "Hello" , "World\n";
}

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        dot();
}
$end = hrtime(true);
$out = "   dot: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$begin = hrtime(true);
for ( $i=0 ; $i<$iterations; $i++) {
        comma();
}
$end = hrtime(true);
$out = " comma: " . round($end-$begin, $precision)."\n";
file_put_contents($file_out, $out, FILE_APPEND);

$ php test.php
...snip...

$ cat benchmark 
   dot: 22893557
 comma: 29056406
无需解释 2024-08-12 19:01:03

还有另一个有趣的区别。根据 @phirschybar 对这个问题的回答,如果您将逗号与 html

 结合使用,您将得到保留换行符的结果。这对于查看格式化对象很有用。例如,给定这个对象:

$object = new stdClass();
$object->property = 'test value';

使用点连接...

echo "<pre>" . var_dump($object) . "</pre>";

...您得到:

object(stdClass)#1 (1) { ["property"]=> string(10) "test value" }<pre></pre>

但是当使用逗号分隔值...

echo "<pre>" , var_dump($object) , "</pre>";

...您得到:

<pre>
object(stdClass)#1 (1) {
  ["property"]=>
  string(10) "test value"
}
</pre>

There is another interesting difference. Per @phirschybar's answer to this question, if you use commas in conjunction with html <pre> you get a result that maintains line breaks. This is useful for viewing a formatted object. For example, given this object:

$object = new stdClass();
$object->property = 'test value';

Using dot concatenation...

echo "<pre>" . var_dump($object) . "</pre>";

... you get:

object(stdClass)#1 (1) { ["property"]=> string(10) "test value" }<pre></pre>

But when using commas to delimit the values...

echo "<pre>" , var_dump($object) , "</pre>";

... you get:

<pre>
object(stdClass)#1 (1) {
  ["property"]=>
  string(10) "test value"
}
</pre>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文