iwato Posted July 27, 2010 Share Posted July 27, 2010 QUESTION: If the $$a and $hello mean the same thing, then why are their values echoed differently in double quotation marks? <?php $a = 'hello'; $$a = 'world'; ?> <?php echo '$a ${$a}'; // $a ${$a} echo '$a $$a'; // $a $$a echo '$a $hello'; // $a $hello echo "$a ${$a}"; // hello world echo "$a $$a"; // hello world echo "$a $hello"; // hello $hello ?> Link to comment Share on other sites More sharing options...
wirehopper Posted July 27, 2010 Share Posted July 27, 2010 PHP processes content between single quotes literally, but it will try to parse the data between double quotes.Take the time to review this page: http://www.php.net/manual/en/language.types.string.phpThere is a performance penalty for double quotes, due to the additional processing. For that reason, use single quotes wherever possible. Link to comment Share on other sites More sharing options...
iwato Posted July 27, 2010 Author Share Posted July 27, 2010 PHP processes content between single quotes literally, but it will try to parse the data between double quotes.Take the time to review this page: http://www.php.net/manual/en/language.types.string.phpThere is a performance penalty for double quotes, due to the additional processing. For that reason, use single quotes wherever possible.Thank you for the performance alert.Fortunately I am not new to your reference page, though. My question was not intended to address the difference between the use of single and double quotes; rather, it was meant to address the difference in the outcomes of the following two echo statements:echo "$a $$a"; // hello worldecho "$a $hello"; // hello $hello Notice that they are both enclosed in double quotation marks!Roddy Link to comment Share on other sites More sharing options...
wirehopper Posted July 27, 2010 Share Posted July 27, 2010 You might want to try {$$a}, as well as trying to output $hello, with and without braces. Link to comment Share on other sites More sharing options...
boen_robot Posted July 27, 2010 Share Posted July 27, 2010 "performance penalty" might be too strong expression for the effect of double quotes... double quotes are slower by less than a milisecond per (literally!) more than a milion such strings. Any PHP script evaluating that much strings is likely to take a lot of time either way, so a difference of a milisecond is not going to make a difference.Still, it's a good practice to use single quotes when you don't need variable interpolation, but not so much for performance's sake than for... let's call it "stability's sake".As for the difference in echo "$a $$a"; // hello worldecho "$a $hello"; // hello $hello Like any variable interpolation - if there's no such variable, the string with the dollar is written out (the second line), and if there is such a variable (The $a in both lines), it's value is written out. In the case of $$a, this means having the value of the $a variable used as the name of the variable to look for.... I'm not sure why you're having different results though, unless maybe you're experiencing a bug in PHP. The variable variables indeed suggests you should have "hello world" in both cases. Link to comment Share on other sites More sharing options...
iwato Posted July 28, 2010 Author Share Posted July 28, 2010 Still, it's a good practice to use single quotes when you don't need variable interpolation, but not so much for performance's sake than for... let's call it "stability's sake".For someone who spends as much time as I with written language that is not related to IT this is not always easy to do.I'm not sure why you're having different results though, unless maybe you're experiencing a bug in PHP. The variable variables indeed suggests you should have "hello world" in both cases.Woe be to me. Woe be to PHP.Roddy Link to comment Share on other sites More sharing options...
wirehopper Posted July 28, 2010 Share Posted July 28, 2010 echo "$a $$a"; // hello worldecho "$a $hello"; // hello $helloI wouldn't use these constructs in this case (even though it's a simple example).For a production system, I'd use:echo $a.' '.$$a;echo $a.' '.$hello;It would be really cool if you'd run a bunch of variations through and then post your code and the results. It's an interesting question. Link to comment Share on other sites More sharing options...
iwato Posted July 28, 2010 Author Share Posted July 28, 2010 You might want to try {$$a}, as well as trying to output $hello, with and without braces.Yeah, I have decided to spend a little more time with simple and curly syntax before moving on to HTTP requests and responses.Thanks, wirehopper.Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.