Asked  7 Months ago    Answers:  5   Viewed   52 times

I am wondering, What is the proper way for inserting PHP variables into a string?

This way:

echo "Welcome ".$name."!"

Or this way:

echo "Welcome $name!"

Both of these methods work in my PHP v5.3.5. The latter is shorter and simpler but I'm not sure if the first is better formatting or accepted as more proper.

 Answers

15

Between those two syntaxes, you should really choose the one you prefer :-)

Personally, I would go with your second solution in such a case (Variable interpolation), which I find easier to both write and read.

The result will be the same; and even if there are performance implications, those won't matter 1.


As a sidenote, so my answer is a bit more complete: the day you'll want to do something like this:

echo "Welcome $names!";

PHP will interpret your code as if you were trying to use the $names variable -- which doesn't exist. - note that it will only work if you use "" not '' for your string.

That day, you'll need to use {}:

echo "Welcome {$name}s!"

No need to fallback to concatenations.


Also note that your first syntax:

echo "Welcome ".$name."!";

Could probably be optimized, avoiding concatenations, using:

echo "Welcome ", $name, "!";

(But, as I said earlier, this doesn't matter much...)


1 - Unless you are doing hundreds of thousands of concatenations vs interpolations -- and it's probably not quite the case.

Tuesday, June 1, 2021
 
hakre
answered 7 Months ago
52

That's strange...

But

<?php
echo '11hello ' . (1 + 2) . '34';
?>

OR

<?php
echo '11hello ', 1 + 2, '34';
?>

fixing issue.


UPDv1:

Finally managed to get proper answer:

'hello' = 0 (contains no leading digits, so PHP assumes it is zero).

So 'hello' . 1 + 2 simplifies to 'hello1' + 2 is 2, because no leading digits in 'hello1' is zero too.


'11hello ' = 11 (contains leading digits, so PHP assumes it is eleven).

So '11hello ' . 1 + 2 simplifies to '11hello 1' + 2 as 11 + 2 is 13.


UPDv2:

http://www.php.net/manual/en/language.types.string.php

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Wednesday, March 31, 2021
 
Corne
answered 9 Months ago
93

You do not need the .. PHP parses double quoted (") strings and replaces the variables with their values. As such:

$pageID = 3;
echo "UPDATE test SET page$pageID = '$pageProgress' WHERE userID = '$userID'";

http://codepad.viper-7.com/uIdqqH

Saturday, May 29, 2021
 
Grzegorz
answered 7 Months ago
24

A good approach would be strtr:

strtr('<h1>{foo}</h1><h2>{bar}</h2>', array(
  '{foo}' => $foo,
  '{bar}' => $bar,
));

You can also use get_defined_vars() to get an array of variables accessible in the current scope, though I personally prefer explicit enumeration.


One of the subtle advantages of using strtr over str_replace is that strtr will not search the already-substituted portions of the string for further replacements. The manual page for str_replace states:

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.

Saturday, May 29, 2021
 
jakubos
answered 7 Months ago
69

In php you can escape variables like so

echo "Hello, ${var}'s head is big";

or like

echo "Hello, {$var}'s head is big";

Reference here under escape character section

Thursday, September 9, 2021
 
Vasiliy
answered 3 Months ago
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