Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php code inside an echo wrapped by a div

how to php inside a div and php inside echo

9:59 am on Mar 21, 2012 (gmt 0)

New User

joined:Mar 21, 2012
posts: 2
votes: 0

Hello i am trying to put this code inside and echo cause i want to be triggered by and if

<div id="remove_all"><a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">Remove</a></div>

so can you please give me a hand
this is where i am stuck

Thanks! George.
I guess it is an easy one but i am struggling on this on
10:36 am on Mar 21, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 17, 2002
votes: 5

You just have to be aware of mixing the single and double quotes - they have to be balanced and in some cases escaped.

Best way to learn this is to split your desired html output into different lines and then see which line triggers the error.

echo '<div id="remove_all">';
echo '<a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">';
echo 'Remove</a></div>';

run that and you will see the error line being the one with $sess. If you still can't spot the error split it up further

echo '<a href="$sess->url($_SERVER['PHP_SELF']';
echo '?page=shop.browse&amp;category_id=';
echo $category_id;
echo ')">';

You should now see which line(s) are not matching single and double quotes. Editing one line like this is much easier than trying to edit a whole line like this

echo '<div id="remove_all"><a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">Remove</a></div>';


If you still can not spot it the problem is with the single quotes around PHP_SELF. They need to be escaped

3:55 pm on Mar 21, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

Another less graceful and not so pretty approach, you can surround other blocks and php calls with php . . . like

<?php if ($filter_selected) { ?>

<p>This will only display if filter selected.</p>

<?php } // end the IF ?>

This allows you to only echo where you need it, and can contain other nested php calls. so you can do

<?php if ($filter_selected) { ?>

<div id="remove_all"><a href="<?php echo $sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id);?>">Remove</a></div>

<?php } // end the IF ?>

IMO this makes for some sloppy programming that's hard to debug but PHP lends itself to it very easily and works in a pinch. Note the big difference is you're not echoing static HTML, just the stuff from PHP - be sure to note the ending ; after the parentheses:

<?php echo $sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id);?> <--- right there

An aside, it seems like calling a class and using PHP_SELF just for the script's name is really a long way to go about what you're doing. PHP_SELF has potential security issues anyway. I'd just define the script name somewhere and do

$thisscript = 'myscript.php';

<?php echo "/$thisscript?page=shop.browse&amp;category_id=$category_id";?>

By using double quotes it allows the scalar variables to interpolate and is much simpler to debug.
4:52 pm on Mar 21, 2012 (gmt 0)

New User

joined:Mar 21, 2012
posts: 2
votes: 0

I must thank you both.

Frank Rizzo thank you for the guide on how find errors.

And rocknbil indeed a <p> can do the job.
and i ll change the PHP_SELF with the domain like this
<?php echo 'http://www.example.com?page=shop.browse&amp;category_id='.$category_id);?>