zelig Posted April 30, 2012 Share Posted April 30, 2012 How can I post this variable $healspell when I already have an echo within a php? Here's what I have: echo '<p>You cast Nature\'s Cure (Recovered <?=$healspell?>)</p>'; It's not printing anything out at the moment, obviously, and if I just put $healspell, it just echos out: $healspell. Thanks! Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/ Share on other sites More sharing options...
Maq Posted April 30, 2012 Share Posted April 30, 2012 You can either use interpolation (which requires double quotes): echo " You cast Nature's Cure (Recovered {$healspell})"; Or concatenation (you can use single or double): echo ' You cast Nature\'s Cure (Recovered ' . $healspell . ')'; There are a few other ways, but these are most common. Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1341797 Share on other sites More sharing options...
zelig Posted April 30, 2012 Author Share Posted April 30, 2012 Ah, nice. Thank you! Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1341818 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 You can either use interpolation (which requires double quotes): echo "<p>You cast Nature's Cure (Recovered {$healspell})</p>"; There are a few other ways, but these are most common. You shouldn't need the {} for this. You'd use it for an array value but a regular variable, no need. Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1341991 Share on other sites More sharing options...
Maq Posted May 1, 2012 Share Posted May 1, 2012 You can either use interpolation (which requires double quotes): echo " You cast Nature's Cure (Recovered {$healspell})"; There are a few other ways, but these are most common. You shouldn't need the {} for this. You'd use it for an array value but a regular variable, no need. I prefer them for readability. But if you want to get picky, you only need them for String keys. Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1342045 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 I prefer them for readability. But if you want to get picky, you only need them for String keys. or object methods. [/picky] <?php class foo { public function bar() { return 'baz'; } } $obj = new foo; echo "How would you like a nice of {$obj->bar()}"; ?> Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1342064 Share on other sites More sharing options...
Maq Posted May 1, 2012 Share Posted May 1, 2012 Good 1. Link to comment https://forums.phpfreaks.com/topic/261864-posting-php-in-an-echo/#findComment-1342079 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.