Jump to content

Undefined Variable


eldan88

Recommended Posts

Hey Guys. I Have a quick questions. I keep getting an "undefined variable" when php parses the following code

 echo "<div class='menu'>${$this->price}</div>"."<br>";

However when I put a space after the dollar sign it works fine...

 echo "<div class='menu'>$ {$this->price}</div>"."<br>";

Doesn't the curly braces tells php to seperate strings from variables? I'm a little bit confused. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/288954-undefined-variable/
Share on other sites

Doesn't the curly braces tells php to seperate strings from variables?

 

No. It tells PHP to interpret $this->price as a variable name:

<?php
 
$var_name = 'foo';
$foo = 'bar';
echo "${$var_name}";    // the same as $foo

See variable variables.

 

You need to escape the dollar sign, which is good practice anyway.

Link to comment
https://forums.phpfreaks.com/topic/288954-undefined-variable/#findComment-1481727
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.