加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 字符串加变量,将变量放入字符串PHP

发布时间:2022-10-31 13:31:59 所属栏目:PHP教程 来源:网络
导读: 我是Laravel的新手,我有这个问题。
我尝试了以下代码行,并且工作正常:return redirect("/cards/{$note->id}");
但是,无论何时我尝试使用单引号,它都不起作用:return redirect('/

我是Laravel的新手,我有这个问题。

我尝试了以下代码行,并且工作正常:return redirect("/cards/{$note->id}");

但是,无论何时我尝试使用单引号,它都不起作用:return redirect('/cards/{$note->id}');

我怎么解决这个问题 ?

使用cards. $note->id

与laravel无关PHP字符串变量,它的基本php内容单引号和双引号

我是新来的,我认为拉拉维(Laravel)可以用自己的方式做这些事情

您首先要做的就是变量插值或字符串插值。 您可以在PHP文档和Wiki上阅读有关它的更多信息。

这是PHP中的一项功能,它允许您传递字符串并在内部解释变量/占位符。

在第二个示例中,您使用的单引号不提供此功能,因此您必须将其分解并将变量手动添加到字符串中:

return redirect('/cards/' . $note->id);

如果您对更详尽的解释及其背后的表现感兴趣,可以在Blizz的此处阅读有关此答案的更多信息。

他的结论是:

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

您应该使用"/cards/{$note->id}"或'/cards/'.$note->id

The most important feature of double-quoted strings is the fact that variable names will be expanded.

When a string is specified in double quotes or with heredoc, variables are parsed within it.

从PHP文档

花括号是字符串插值的一部分,完全不应包含在单引号中。

像这样使用它:

return redirect('/cards/'. $note->id);

用单引号或双引号

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!