PHP:在双引号内使用变量 [英] PHP: Using a variable inside a double quotes

查看:705
本文介绍了PHP:在双引号内使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查以下代码.

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

$imagebaseurl是一个变量,包含指向我的图像文件夹的链接(上载),并且在该文件夹内,我还有一些其他文件夹,这些文件夹以我的用户名命名.例如:我有一个名字叫john的用户,因此链接应该看起来像这样-> support/content_editor/uploads/john.

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.

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

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.

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

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)

现在,您能告诉我如何在此$imagebaseurl = 'support/content_editor/uploads/$name';

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;

$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";

尽管要养成在双引号中使用{$...}而不是仅在$...中使用的习惯,但是在某些情况下,您需要在字符串中插入变量,而对于PHP来说这并不明显,这是变量的一部分,哪一部分是字符串.

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.

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

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

这篇关于PHP:在双引号内使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆