Thursday 11 March 2021

Php: Single vs double quotes

Both single and double quotes are used to define strings. But there is one significant difference, strings in double quotes are processed by PHP before being outputted.

 

Example

you can interpolate the variable value in double quotes, but not in single quotes.

 

single_double_quote_demo.php

#!/usr/bin/php

<?php

    $name = "Krishna";
    $age = 10;
    
    $info1 = "Hello Mr.$name, you are $age years old\n";
    $info2 = 'Hello Mr.$name, you are $age years old';
    
    echo $info1;
    echo $info2;
?>

 

Output

$./single_double_quote_demo.php 

Hello Mr.Krishna, you are 10 years old
Hello Mr.$name, you are $age years old

 

Note

Since there is not pre-processing step for strings in single quotes, they work little more efficient as compared to double quoted strings.

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment