Forum Moderators: coopster

Message Too Old, No Replies

php code inside echo

         

NeRu

3:30 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



when i write a code like that

<?php if($_SESSION['user']!= admin) { echo "<a href=\"userprofile.php?userID=<?php echo ($query['userID']);?>\">User</a>"; }?>

i have an error message which indicates the code's line as "Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING"

how can i a php code inside an echo tag?

RussellC

3:39 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



This should work.

<?php
if ($_SESSION['user']!= admin) {
echo "<a href=\"userprofile.php?userID=$quer['userID']\">User</a>";
}
?>

drogyn

4:14 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Or you could avoid an echo by using something like:

<?php
if($_SESSION['user']!= admin)
{
?>

<a href="userprofile.php?userID=<?php echo ($query['userID']);?>">User</a>

<?php
}

Salsa

4:35 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



As Russell alluded to, there is no need ot open <?php again and echo again. That will only break stuff...you are already echoing.

Drogyn's suggestion would work too, but it's not quite as clean.

However, Russell's example might not work for you because, when your variable is an element of an array, you need to concatinate it like this:

echo "<a href=\"userprofile.php?userID=".$quer['userID']."\">User</a>";

...note that the variable is inside of ". and ."

Or you can put the variable in {} like:

echo "<a href=\"userprofile.php?userID={$quer['userID']}\">User</a>";

But the way Russel showed you would work if you were using a simple variable like:

echo "<a href=\"userprofile.php?userID=$userID\">User</a>";

I hope this helps,
Salsa
_______

NeRu

5:36 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



thanks for your helps. it works well ;)