" vs ' in php

jlknauff

New member
Aug 25, 2008
237
1
0
Can someone tell me the difference between the two as it relates to php? They seem to be interchangeable in most cases.
 


" allows string interpolation. ' does not. Both represent strings so are able to be used interchangeably.
 
" allows string interpolation. ' does not. Both represent strings so are able to be used interchangeably.

This. Also " is much slower than ' because of the interpolation. Its faster (execution wise) to just concatenate the value to the string.
 
Fuck knows what you all mean by interpolation :) , but the only differences I am aware of (0.000000000000000001 of a second quicker processing aside)

Code:
$someLine."\r\n"; // will work as expected
$someLine.'\r\n'; // will not work as expected

"hello $name what do you want"; // lazy echo variables
'hello '.$name.' what do you want'; // too many damn .s and 's for my liking
 
Interpolation is what you are referring to as "lazy echo variables".

Code:
$variable = 'cat';

echo "Here we have a $variable."; // Output will be "Here we have a cat."

echo 'Here we have a $variable.'; // Output will be "Here we have a $variable."

echo 'Here we have a ' . $variable . '.'; // Output will be "Here we have a cat."

The final example does seem difficult to read, but if your editor has syntax highlighting it's pretty straight forward. I always do it that way.
 
Might as well select on coding style preference and be consistent.

Switching between single and double marks in one codebase is revolting.