PHP:在双引号内使用变量

时间:2022-09-15 16:22:18

please check the following code.

请检查以下代码。

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

The $imagebaseurl is 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 $name in 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 $name in 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 $name in this $imagebaseurl = 'support/content_editor/uploads/$name';

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

2 个解决方案

#1


38  

$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.

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

#2


0  

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.

为什么?第一个可能看起来更清楚,但第二个更可重复使用。我推荐使用sprintf而不是php magic double quote nonesense,字面意思是,没有其他语言存在。

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

#1


38  

$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.

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

#2


0  

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.

为什么?第一个可能看起来更清楚,但第二个更可重复使用。我推荐使用sprintf而不是php magic double quote nonesense,字面意思是,没有其他语言存在。

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