PHP Quotes and Apostrophes – the Difference

Many people seem to be confused with the use of quotes and apostrophes when coding in PHP.

Using apostrophes is the lightweight way. Use these when you just want to create a simple string:

$var = 'This is just plain old text.';

Using quotes will provide you with a bit more functionality and is slightly more expensive in execution:

// Use \n to add a special new line character
$var = "This is the first line\nThis is the second line";

// Include the value of a variable
$var = 'bones';
echo "Don't rattle my $var";

Please also note that PHP will evaluate any text in quotes – it is, therefore, more efficient to use apostrophes by default and use quotes only when you need them.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *