manalnor Posted August 31, 2010 Share Posted August 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212185-php-code-inside-echo-here/ Share on other sites More sharing options...
Pikachu2000 Posted August 31, 2010 Share Posted August 31, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/212185-php-code-inside-echo-here/#findComment-1105660 Share on other sites More sharing options...
KevinM1 Posted August 31, 2010 Share Posted August 31, 2010 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']). Quote Link to comment https://forums.phpfreaks.com/topic/212185-php-code-inside-echo-here/#findComment-1105661 Share on other sites More sharing options...
Pikachu2000 Posted August 31, 2010 Share Posted August 31, 2010 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/212185-php-code-inside-echo-here/#findComment-1105663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.