MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP String interpolation

You can also use interpolation to interpolate (insert) a variable within a string. Interpolation works in double quoted strings and the heredoc syntax only.


$name = 'Joel';
// $name will be replaced with `Joel`
echo "<p>Hello $name, Nice to see you.</p>";
#> "<p>Hello Joel, Nice to see you.</p>"
// Single Quotes: outputs $name as the raw text (without interpreting it)
echo 'Hello $name, Nice to see you.'; # Careful with this notation
#> "Hello $name, Nice to see you."

The complex (curly) syntax format provides another option which requires that you wrap your variable within curly braces {}. This can be useful when embedding variables within textual content and helping to prevent possible ambiguity between textual content and variables.


$name = 'Joel';
// Example using the curly brace syntax for the variable $name
echo "<p>We need more {$name}s to help us!</p>";
#> "<p>We need more Joels to help us!</p>"
// This line will throw an error (as `$names` is not defined)
echo "<p>We need more $names to help us!</p>";

The {} syntax only interpolates variables starting with a $ into a string. The {} syntax does not evaluate arbitrary PHP expressions.


// Example tying to interpolate a PHP expression
echo "1 + 2 = {1 + 2}";

Output

"1 + 2 = {1 + 2}"

// Example using a constant
define("HELLO_WORLD", "Hello World!!");
echo "My constant is {HELLO_WORLD}";
#> "My constant is {HELLO_WORLD}"
// Example using a function
function say_hello() {
 return "Hello!";
};
echo "I say: {say_hello()}";

Output

"I say: {say_hello()}"

However, the {} syntax does evaluate any array access, property access and function/method calls on variables,
array elements or properties:


// Example accessing a value from an array — multidimensional access is allowed
$companions = [0 => ['name' => 'Amy Pond'], 1 => ['name' => 'Dave Random']];
echo "The best companion is: {$companions[0]['name']}";

Output

"The best companion is: Amy Pond"

// Example of calling a method on an instantiated object
class Person {
 function say_hello() {
 return "Hello!";
 }
}
$max = new Person();
echo "Max says: {$max->say_hello()}";

Output

"Max says: Hello!"

// Example of invoking a Closure — the parameter list allows for custom expressions
$greet = function($num) {
 return "A $num greetings!";
};
echo "From us all: {$greet(10 ** 3)}";

Output

"From us all: A 1000 greetings!"

Notice that the dollar $ sign can appear after the opening curly brace { as the above examples, or, like in Perl or Shell Script, can appear before it:


$name = 'Joel';
// Example using the curly brace syntax with dollar sign before the opening curly brace
echo "<p>We need more ${name}s to help us!</p>";

Output

We need more Joels to help us!


The Complex (curly) syntax is not called as such because it's complex, but rather because it allows for the use of 'complex expressions'.



Conclusion

In this page (written and validated by ) you learned about PHP String interpolation . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Extracting replacing substrings.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.