You are hereHome / Development / PHP: Beware of Variables Inside Strings

PHP: Beware of Variables Inside Strings


By Gerd Riesselmann - Posted on 28 February 2007

One of the rather useless discussion among software developers is if to use PHP variable parsing when building string or not. That is if you should write your code like this:

<?php
$value
= 15;
$text = "Value is $value, isn't it?";
?>

Or better like this?

<?php
$value
= 15;
$text = 'Value is ' . $value . ', isn\'t it?';
?>

Well, some claim that the first version, using variables inside strings, is slower, while the others point out the second code is less readable.

A matter of taste, I think, since both readable code and performance are important some way or another.

However, this PHP memory bug gives me the creeps: The first code will not free the memory used until the end of script execution. So you better don't use code like this within loops.