Jump to content

PHP Code inside echo 'here';


manalnor

Recommended Posts

Hello friends,

 

if i've the following php code

 

echo 'something';

 

and i want to add php code for example ( call name from db )

 

<?=$ama[name]?>

 

how to write it :

 

i've tried this but didn't worked

 

echo 'something $ama[name] ';

 

can somone please show me how to write varibles inside echo.

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/212185-php-code-inside-echo-here/
Share on other sites

Anything in single quotes is interpreted as a string literal. You need to use double quotes, or string concatenation to output the value of the variable.

 

$variable = 'rest of the string';
echo "This is the $variable.";
echo 'This is the ' . $variable;

Array variables work different than 'normal' variables within a double-quoted string.  Normally, something like:

 

echo "Some value: $value";

 

Works just fine.  For array variables, you need to stick them in curly braces for it to work, like so:

 

echo "Look, it's an array variable: {$array[$key]}";

 

In your case, name needs to be put in single-quotes ($ama['name']).

Array variables work different than 'normal' variables within a double-quoted string.  Normally, something like:

 

echo "Some value: $value";

 

Works just fine.  For array variables, you need to stick them in curly braces for it to work, like so:

 

echo "Look, it's an array variable: {$array[$key]}";

 

In your case, name needs to be put in single-quotes ($ama['name']).

 

Oh, yeah. I seem to have missed that part . . .  :-[

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.