Open In App

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

Last Updated : 10 Apr, 2022
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”



Similar Reads

Difference between single-quoted and double-quoted strings in JavaScript
In this article, we will see the difference between single-quoted and double-quoted strings in JavaScript. Both single-quotes and double-quotes strings in JavaScript are used for creating string literals. But the basic difference between them comes into play when the character which needed to be escaped is itself a single-quoted or double-quoted st
5 min read
Difference between Double and Single Curly Brace in AngularJS ?
In the AngualrJS framework, we can build attractive and dynamic web applications through different utilities. In AngularJS, there are double curly braces "{{ }}" and also single curly braces "{ }". The Double Curly Braces in AngularJS are mostly used for Data Binding, which also means that we are allowed to insert dynamic content into our HTML temp
4 min read
What is the use of Single Quotes ('') & Double Quotes ("") for Strings in JavaScript ?
In JavaScript, both single quotes ('') and double quotes ("") can be used to create strings, and they are interchangeable in most situations. However, there are a couple of differences worth noting: Quoting Inside Strings: If your string contains an apostrophe (single quote) and you use double quotes to define the string, you won't need to escape t
1 min read
How to add section that is quoted from another source using HTML ?
The &lt;blockquote&gt; tag in HTML is used to display the long quotations (a section that is quoted from another source). It changes the alignment to make it unique from others. It contains both opening and closing tags. Syntax: &lt;blockquote&gt; Contents... &lt;/blockquote&gt; Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt;
1 min read
When should we use double or single quotes in JavaScript ?
This article aims to clarify when you should use double or single quotes while coding in JavaScript. If you have worked with JavaScript, you may know that it allows expressing string literals with both double quotes (“) and single quotes (‘). There are many differences between double and single quotes in other programming languages. In JavaScript,
3 min read
Difference between double equal vs triple equal JavaScript
Double equal: The double equal('==') operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison. Triple equal: The triple equal('===') operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return fa
2 min read
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Equal Operator == The comparison operator called Equal Operator is the double equal sign "==". This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same. This should always be kept in mind that the
2 min read
Double not (!!) operator in PHP
The "NOT NOT" operator or Double not(!!) operator in PHP simply returns the truth value of the variable or expression. To explain in very simple terms, the first not operator(!) negates the expression. The second not operator(!) again negates the expression resulting the true value which was present before. The (!!) operator returns as that Boolean
2 min read
What does $$ (dollar dollar or double dollar) means in PHP ?
The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value. These are called variable variables in PHP. Variable Variables:- Variable variables are simply va
2 min read
How to Convert Int Number to Double Number in PHP ?
In PHP, working with numeric data types is a common task, and sometimes you might need to convert an integer (int) to a double (float). In this article, we will explore various approaches to achieve this conversion in PHP, covering different methods based on the PHP version and use cases. Table of Content Using Type CastingUsing floatval() Function
2 min read