29

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.

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)

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

hakre
200k55 gold badges453 silver badges865 bronze badges
asked May 9, 2012 at 8:29
2
  • 2
    Use double-quotes instead of single ones or use Radu's solution. Commented May 9, 2012 at 8:31
  • 2
    (' is a single-quote, not a double-quote...) Commented May 9, 2012 at 8:32

2 Answers 2

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

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

answered May 9, 2012 at 8:30
Sign up to request clarification or add additional context in comments.

3 Comments

Just a sidenote: it's recommended not to use double quotes with variables inside them. It's horrible for code readability and debugging and PHP is also slightly faster if single quotes are used (like the first example of Radu's answer).
Note that you can use single quotes inside double quotes and still parse the variable <?php $test = 'test'; echo "This is a '$test'.";.
Personally I usually find the string interpolation approach to be much more readable than lots of expressions concatenated together with the dot operator. But, how bad is the performance hit of using this? Anyone benchmark it? :)
-7

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();

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.

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

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

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

answered May 9, 2017 at 20:59

3 Comments

@b_dubb I’m hardly skilled at php specifically, but php supports several things other languages don’t, and Humble here provides no reason that magic quotes are inferior to what is a very similar thing with sprintf, as well as ignored how much more cumbersome it is to write and maintain sprintf strings—especially those with multi-part. I don’t know who downvoted though.
Python 3.6 introduces Formatted string literals (docs.python.org/3/reference/lexical_analysis.html#f-strings).
I added another downvote and here's why: First of all, this language feature is called 'string interpolation', and not only is it false to say it exists in "NO other language", but you can look here and see over a dozen examples of it: en.wikipedia.org/wiki/String_interpolation. It's an extremely helpful feature, especially for the kinds of tasks a language like PHP is designed for. Another reason for the downvote is: what about this code is 're-usable'? What does reusability even have to do with this issue? Further, the answer is overly opinionated and frankly hostile in tone.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.