Home >Backend Development >PHP Tutorial >How Can I Echo a Variable Inside Single Quotes in PHP?
Echoing Variables within Single Quotes
It is not possible to directly echo a variable within single quotes. This is because PHP interprets the single quotes as the delimiter for the literal string, and the variable is not interpolated.
Solutions:
To include a variable within single quotes, you have two options:
Concatenation: Use the concatenation operator ('.') to append the variable to the string:
echo 'I love my ' . $variable . '.';
Double Quotes: Use double quotes instead of single quotes. Double quotes allow variable interpolation, meaning that PHP will automatically replace the $variable with its value:
echo "I love my $variable.";
The above is the detailed content of How Can I Echo a Variable Inside Single Quotes in PHP?. For more information, please follow other related articles on the PHP Chinese website!