Welcome to WebmasterWorld Guest from 3.226.251.81

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php code inside echo

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

New User

10+ Year Member

joined:Jan 26, 2006
posts:9
votes: 0


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?

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


This should work.

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

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

New User

10+ Year Member

joined:Jan 26, 2006
posts:4
votes: 0


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
}

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

Full Member

10+ Year Member

joined:Oct 9, 2003
posts:314
votes: 0


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
_______

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

New User

10+ Year Member

joined:Jan 26, 2006
posts:9
votes: 0


thanks for your helps. it works well ;)