We’ll look at PHP string interpolation with an example of best practices in this article. Developers can directly embed variables into strings with the help of string interpolation, which also makes variable concatenation easier and makes code easier to read.
The process of evaluating variables and expressions within a string and replacing placeholders with their actual values is known as string interpolation. PHP supports two main methods of string interpolation: double-quoted strings and heredoc syntax.
Sample Example Using concatenation:
$f_name = "Adam"; $l_name = "Joe"; $message = "Hello, My Name is " . $f_name . " " . $l_name . ".";
Using string interpolation:
$message = "Hello, You are first name $f_name and last name is l_name.";
in the above code, We have two variables $name
and $age
, and directly embedded within the string using the $
symbol.
Single-quoted strings do not support variable interpolation in PHP. You can use .
operator in single-quoted strings to concatenate variables with the string.
$f_name = "Adam"; $l_name = "Joe"; $message = 'Hello, You are first name'. $f_name .and last name is '.$l_name; echo $message;
Output:
Hello, You are first name Adam and last name is Joe.
Let’s start String Interpolation in PHP with some examples and discuss the benefits of it over string concatenation. Here, We’ll cover the following ways to interpolate string:
You can use double quotes for string interpolation in PHP. When a string is enclosed in double quotes, PHP will replace variables with their values within the string. Here’s an example:
$f_name = "Adam"; $l_name = "Joe"; $message = "Hello, You are first name $f_name and last name is $l_name."; echo $message;
Output:
Hello, You are first name Adam and last name is Joe.
You can use Heredoc syntax to do string interpolation in PHP. Heredoc allows you to specify a multi-line string without the need for single or double quotes while maintaining all of the whitespace, including line breaks.
$f_name = "Adam"; $l_name = "Joe"; $message = <<<EOT Hello, my name is $f_name $l_name. This is a multiline string using heredoc. EOT; echo $message;
In this example, <<<EOT
starts the heredoc string, and EOT
; ends it. The variables $f_name
and $l_name
are interpolated within the heredoc string. The output will be:
Hello, my name is Adam Joe. This is a multiline string using heredoc.
Heredoc is useful whenever you need to maintain formatting without escaping special characters in large text blocks or multiline strings.
Let’s discuss some examples of string interpolation using PHP.
You can also escape special characters in the time of interpolation. The variables contain characters like quotes or backslashes treated as a normal characters.
$message = "Hello! I am adam!"; echo "The message is: \"$message\"";
Output:
The message is: "Hello! I am adam!"
It’s better to use curly braces to clearly define the variable scope for more complex expressions inside interpolated strings.
$n1 = 20; $n2 = 10; echo "The sume are: \${$n1 * $n2}";
Output:
The sum is: 40
We have learned about string interpolation in PHP. You can use string concatenation for simple string manipulation, but for more complex strings you need to use double-quoted or heredoc syntax to manipulate string. The string interpolation enhances code readability and maintainability.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More