Asked  8 Months ago    Answers:  5   Viewed   53 times

Is it better to concatenate a variable (say, $name) into an existing string (say, $string) like this:

$string='Hi, my name is '.$name

or to embed the variable in the string like this:

$string="Hi, my name is $name";

or is it better to use a function like this:

$string=sprintf("Hi, my name is %s",$name);

Which is better in terms of processor time/efficiency?

 Answers

62

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.

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)

Wednesday, March 31, 2021
 
Jeff
answered 8 Months ago
24

+ and . have the same operator precedence, but are left associative. Means after the first concatenation:

'(' . (int)$data['event_id']

The string got added with your key, e.g.

"($data['event_id']" + $key

So the string gets converted into an integer in that numerical context and disappears. To solve this use parentheses () around your addition.

Wednesday, March 31, 2021
 
IvanH
answered 8 Months ago
13

I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed.

For example:

LIGHT_MESSAGES = {
    'English': "There are %(number_of_lights)s lights.",
    'Pirate':  "Arr! Thar be %(number_of_lights)s lights."
}

def lights_message(language, number_of_lights):
    """Return a language-appropriate string reporting the light count."""
    return LIGHT_MESSAGES[language] % locals()

def is_pirate(message):
    """Return True if the given message sounds piratical."""
    return re.search(r"(?i)(arr|avast|yohoho)!", message) is not None
Tuesday, June 1, 2021
 
tiny
answered 5 Months ago
23

The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than one parameter, and will print each one.

So your first example is calling echo with more than one parameter, and they are all being printed, vs. the second example where all the strings are being concatentated and that one big string is being printed.

Friday, August 6, 2021
 
mounaim
answered 3 Months ago
25

You could save that kind of string by modifying it on the server side before output, like this:

html_content = "<?=addslashes($string);?>"

The method addslashes($string) would then escape any double quotes at runtime before feeding it to the JavaScript variable.

Thursday, October 21, 2021
 
c_k
answered 1 Week ago
c_k
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share