Jump to content
Larry Ullman's Book Forums

Why Will Code Not Work? - But It Does!


Recommended Posts

Hi Larry,

 

I was going through the end of chapter questions for Chapter 4.  The second bullet point asks why the following code won't work?  

print "The total is $$total";

 

However when I run that code it does work, by which I mean it prints out "The total is 10" when $total = 10.  

 

I am running windows 10, PHP 7.1.9

 

 

Link to comment
Share on other sites

Sorry about the confusion! My expectation is this is due to a change in more recent versions of PHP. In older versions, it would have treated $$total as a "variable variable", but it looks like it's being smarter now.

 

A variable variable would work like this:

$cat = 'dog';

$dog = 42;

print $$cat; // Prints 42 b/c it takes the value of $cat--"dog"--and inserts that to create "print $dog".

 

Sorry about that!

Link to comment
Share on other sites

 Share

×
×
  • Create New...