Open In App

What is the difference between single-quoted and double-quoted strings in PHP?

Improve
Improve
Like Article
Like
Save
Share
Report

Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having any special meaning. Single-quote is usually faster in some cases. The special case is that if you to display a literal single-quote, escape it with a backslash (\) and if you want to display a backslash, you can escape it with another backslash (\\)

Below program illustrates the Single-quoted Strings: 

Program 1: 

php




<?php
 
// A simple string
echo 'I am a geek. ';
echo"\n";
 
// use of back-slash to display string with apostrophe
echo 'It\'ll be interesting to know about the string. ';
echo"\n";
 
// to escape the backslash within the string
echo 'A \\ is named as backslash. ';
echo"\n";
 
// The variable will not be evaluated if used directly
//within the single-quoted string
$string = 'geeks';
echo 'This is a portal for $string. ';
echo"\n";
 
// The \n will be displayed as it is without having any
//special meaning.
echo 'This is a portal for \n geeks. ';
?>


Output:

I am a geek. 
It'll be interesting to know about the string. 
A \ is named as backslash. 
This is a portal for $string. 
This is a portal for \n geeks.

Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value. 

Below program illustrates the Double-quoted Strings: 

Program 2: 

php




<?php
 
// A simple string.
echo "I am a geek.";
 
echo"\n";
 
// Nothing extra is needed here
echo "It'll be interesting to know about the string.";
 
echo"\n";
 
// \n is working here
echo "This is a simple string.";
 
echo"\n";
 
// Variables are included directly
$string = 'ABC';
echo "The word is $string.";
 
?>


Output:

I am a geek.
It'll be interesting to know about the string.
This is a simple string.
The word is ABC.

Let us understand the differences in a tabular form -:

  Single-Quoted Strings Double-Quoted Strings
1. It is a way to specify a string in PHP It is also a way to specify a string in PHP
2.

Syntax -:

‘string’

Syntax -:

“string”

3. In Single-Quoted strings, the Escape sequence does not expands In Double-Quoted strings, the Escape sequence expands.
4. In Single-Quoted strings, the variable’s name does not expands In Double-Quoted strings, the variable’s name also expands
5.

Example -:

‘GeeksforGeeks’

Example -:

“GeeksforGeeks”



Last Updated : 10 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads