Jump to content

Posting php in an echo


zelig

Recommended Posts

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

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.

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.

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.

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()}";

?>

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.