Php echo variable in quotes

The most significant difference between the single and the double quotes lies when we interpolate the string and the variable. The single quote does not interpolate the string and the variables. The content inside the single quote prints out as exactly as it is. In most cases, there is no compilation of any variables or escape sequences inside the single quote

But, in the case of the double quote, the variable written inside the quotes will be interpolated with the string. It means that the variable in the string will be evaluated. Consequently, it is easy to use double quotes when interpolating the string and the variables. The advantage of double quotes over single quotes is that we need not concatenate the string and the variables using the . operator. However, as the variables need to be evaluated in the string, using double quotes will be slightly slower than using the single quote.

For example, create a variable $name and write the string Bond to it. Next, write the string The name is $name. and enclose the string with double-quotes. Use the echo function to print the string. Similarly, enclose the exact string with a single quote in the following line.

Example Code:

Output:

The name is Mustafa.
The name is $name.

This section will discuss the escape sequences using double quotes and single quotes in PHP. The escape sequences can be used inside the double quotes to escape the characters. Meanwhile, most escape sequences are not interpreted inside a single quote. However, there is an exception for escaping the apostrophe.

There is no need to escape the apostrophe if we write an apostrophe in the string while using double-quotes. But, in the case of a single quote, we need to escape it using \'. The example is shown below.

Example Code:

Output:

Mustafa Ahmed's  "Think and grow rich".
Mustafa Ahmed's \"Think and grow rich\".

In the above code, it is obvious that the apostrophe must not escape using the double-quotes. The double quotes are also escaped inside the double quotes using the character escapes. Meanwhile, in the case of the single quote, the apostrophe is escaped using the character escape. In the latter case, the double quotes are not escaped while using the character escapes.

These are the significant differences between the single and double quotes in PHP.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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

    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

    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”


    How do you echo quotes in PHP?

    Escape Quotation Marks in PHP.
    Use the Backslash \ Before the Quotation to Escape the Quotation Marks..
    Use the Heredoc Syntax

    Bài mới nhất

    Chủ Đề