php string interpolation – PHP Variable Interpolation

php string interpolation – PHP String Interpolation, Using Variables in Strings. Use the string concatenation operator (.) when the value you want to include can’t be inside the string:

PHP string interpolation syntax

$user_str = 'and some more text';
$results = "This variable contains some text, $user_str";

// Result the interpolated variable
echo $results;

php string interpolation and Expressions Within Strings

print 'You have '.($_REQUEST['products'] + $_REQUEST['items']).' companmy.';
print "The word '$word' is ".strlen($word).' characters long.';
print 'You owe '.$amounts['payment'].' immediately';
print "My love's diameter is ".$love->getDiameter().' inches.';

We PHP, a string literal can be specified in main 4 ways,

  • Single quoted
  • Double quoted
  • Heredoc syntax
  • Nowdoc syntax

Variable interpolation in double quoted string

$websites = "PAKAINFO";
echo "we have website $websites"; // results: we have website PAKAINFO
echo 'we have website $websites'; // results: we have website $websites

Variable interpolation with heredoc

$websites = "PAKAINFO";
$myDoc = <<< EOD we have website $websites
to know all about PHP 
EOD; 
echo $myDoc;

Interpolating variable in a word (complex / curly syntax)

$websites = "Paka";
echo "we have website {$websites}INFO"; // results: we have website PAKAINFO

String interpolation

Example

$websites = 'infinityknow';

// $websites will be replaced with `infinityknow`
echo "

Welcome $websites, Nice to see you.

"; # ↕ #> "

Welcome infinityknow, Nice to see you.

" // Single Quotes: outputs $websites as the raw text (without interpreting it) echo 'Welcome $websites, Nice to see you.'; # Careful with this notation #> "Welcome $websites, Nice to see you."
$websites = 'infinityknow';

// Example using the curly brace syntax for the variable $websites
echo "

We need more {$websites}s to help us!

"; #> "

We need more infinityknows to help us!

" // This line will throw an error (as `$websites` is not defined) echo "

We need more $websites to help us!

"; #> "Notice: Undefined variable: websites"

Example tying to interpolate a PHP expression

echo "1 + 2 = {1 + 2}";
#> "1 + 2 = {1 + 2}"

define("Welcome_PAKAINFO", "Welcome PAKAINFO!!");
echo "My website is {Welcome_PAKAINFO}";
#> "My website is {Welcome_PAKAINFO}"


function getMessage() {
    return "Welcome!";
};
echo "I say: {getMessage()}";
#> "I say: {getMessage()}"

Example accessing a value from an array — multidimensional access is allowed

$companions = [0 => ['websites' => 'Pakainfo Com'], 1 => ['websites' => 'Infinityknow']];
echo "The best companion is: {$companions[0]['websites']}";
#> "The best companion is: Pakainfo Com"

class Product {
  function getMessage() {
    return "Welcome!";
  }
}

$max = new Product();

echo "Max says: {$max->getMessage()}";
#> "Max says: Welcome!"

$greet = function($num) {
    return "A $num greetings!";
};
echo "From us all: {$greet(10 ** 3)}";
#> "From us all: A 1000 greetings!"

PHP String Concatenation Example

$websites = 'infinityknow';

echo "

We need more ${websites}s to help us!

"; #> "

We need more infinityknows to help us!

"

I hope you get an idea about php string interpolation.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment