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

In PHP, single-quoted strings and double-quoted strings are very similar, with a few key differences.

The biggest difference between single-quoted and double-quoted strings is that single-quoted strings are slightly faster and use slightly less memory. This is because single-quoted strings don't support string interpolation, which means that you cannot use variables or special characters inside a single-quoted string and have them interpreted as their corresponding values. Instead, the variables and special characters are treated as plain text.

Watch a course Learn object oriented PHP

On the other hand, double-quoted strings do support string interpolation, which means that you can use variables and special characters inside the string and they will be interpreted as their corresponding values. However, this means that double-quoted strings are slightly slower and use slightly more memory than single-quoted strings.

Here's an example to illustrate the difference between single-quoted and double-quoted strings:

<?php

$num = 10;

// This is a single-quoted string
$str = 'The value of $num is $num';

// This is a double-quoted string
$str = "The value of $num is $num";

// Outputs: The value of $num is 10
echo $str;

In the example above, the single-quoted string 'The value of $num is $num' is interpreted as a literal string, so the variable $num is not replaced with its value. On the other hand, the double-quoted string "The value of $num is $num" is interpreted as a string that contains variables, so the variable $num is replaced with its value, 10.