Question:
How to Display a PHP Variable in HTML in an Echo?
2010-11-28 15:56:45 UTC
I have the following part of a PHP code below.

echo '
echo $rows['name'];echo $rows['lastname'];echo $rows['email'];
';

I am trying to echo a row from a database, but it's not working. How can I echo the $row variable in the code?
Four answers:
just "JR"
2010-11-29 02:23:28 UTC
The only thing Azrael forgot to mention is WHY echo '$variable' is wrong!



PHP does NOT parse variables enclosed in SINGLE QUOTES.
2010-11-28 18:09:29 UTC
You can continue using single-quotes throughout the entire code but you just have to escape the current string sequence



echo '
echo $rows['name'];'.$rows['lastname'].'echo $rows['email'];
';



Notice I took out the echo right before $rows. This is because you already started the echo before . Also i used a single-quote to close the current string and then a period to begin concatenation. Next is concatenating the rest of the sting by using a period followed by a single-quote.
2010-11-28 16:02:25 UTC
I'm answering this from top of my head so it may not be accurate. But try something like this.

echo "
". $rows['name']; . "". $rows['lastname']; ."". $rows['email']; . "
";
azrael-sub7
2010-11-28 16:39:38 UTC
echo "$variable" ; // OK

echo $variable; // OK

echo '$variable' ; // Wrong

echo " ".$variable." ";// OK

echo ' '$variable.' '; //OK



So you can :



Method 1 :

echo "
$rows['name']$rows['lastname']$rows['email']
";



Method 2 :



echo "
$rows['name']$rows['lastname']$rows['email']
";



Method 3



echo "
" . $rows['name'] . "" . $rows['lastname'] . "" . $rows['email']. "
";



Method 4



echo "
"; echo $rows['name'] . "" . $rows['lastname'] . ""; echo $rows['email'] . "
";



There are at least 12 methods doing this with the echo function .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...