PHP:在双引号内使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10512452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:21:52  来源:igfitidea点击:

PHP: Using a variable inside a double quotes

php

提问by black_belt

please check the following code.

请检查以下代码。

$imagebaseurl = 'support/content_editor/uploads/$name';

The $imagebaseurlis a variable that is containing a link to my image folder (uploads) and inside the folder I have some other folders which are named after my users name. for example: I have a user who's name is john, so the the link should look like this-> support/content_editor/uploads/john.

$imagebaseurl是一个变量,其中包含指向我的图像文件夹(上传)的链接,并且在文件夹内我还有一些其他文件夹,这些文件夹以我的用户名命名。例如:我有一个名为 john 的用户,因此链接应如下所示 -> support/content_editor/uploads/john

The main idea is when any user is logged in and browses his image gallery I want to take him to his own gallery which basically is named after his name.

主要思想是当任何用户登录并浏览他的图片库时,我想带他到他自己的画廊,该画廊基本上以他的名字命名。

When he will visit the gallery the value of $namein the link will come from the user's login name (from session). Now the problem is as you probably have already understood that the placement of $namein the above link is wrong and that is why it is not working. I am getting this whole URL> (support/content_editor/uploads/$name) instead of (support/content_editor/uploads/john)

当他访问图库$name时,链接中的值将来自用户的登录名(来自会话)。现在的问题是,您可能已经明白,$name上面链接中的位置是错误的,这就是它不起作用的原因。我得到了整个 URL> (support/content_editor/uploads/$name) 而不是 (support/content_editor/uploads/john)

Now could you please tell me how to use the $namein this $imagebaseurl = 'support/content_editor/uploads/$name';

现在,请你告诉我如何使用$name这个$imagebaseurl = 'support/content_editor/uploads/$name';

回答by rid

$imagebaseurl = 'support/content_editor/uploads/' . $name;

or

或者

$imagebaseurl = "support/content_editor/uploads/{$name}";

Note that if you use double quotes, you can also write the above as:

注意,如果使用双引号,也可以将上面的写成:

$imagebaseurl = "support/content_editor/uploads/$name";

It's good though to get in the habit of using {$...}in double quotes instead of only $..., for times where you need to insert the variable in a string where it's not obvious to PHP which part is the variable and which part is the string.

养成使用{$...}双引号而不是仅使用双引号的习惯是件好事$...,因为有时您需要将变量插入字符串中,而 PHP 不清楚哪部分是变量,哪部分是字符串。

If you want the best performance, use string concatenation with single quotes.

如果您想要最佳性能,请使用带单引号的字符串连接。

回答by HumbleWebDev

I couldn't disagree more with the previous post.

我不能更不同意上一篇文章。

I'd almost go as far to call it bad practice to use

我几乎会说使用它是不好的做法

$varname = 'EXAMPLE';

$fulltext = "This is an $varname";

Its more maintainable, from my experience, to utilize a good friend of mine known as sprintf();

根据我的经验,使用我的一个好朋友 sprintf() 更易于维护;

define('CONST_NAME', 'This is an example of a better %s');
define('EXAMPLE', sprintf(CONST_NAME, 'practice'));

echo EXAMPLE;

Why? The first one may seem more clear, but the second one is much more re-usable. I recommend utilizing sprintf over the php magic double quote nonesense which exists, literally, in NO other language.

为什么?第一个可能看起来更清晰,但第二个更易于重复使用。我建议在 php 魔术双引号废话上使用 sprintf,从字面上看,它不存在于其他语言中。

http://php.net/manual/en/function.sprintf.php

http://php.net/manual/en/function.sprintf.php

This is also the practice used very similar to C#, and many other languages.

这也是与 C# 和许多其他语言非常相似的做法。

See also: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

另见:https: //msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx