echo and "<!--"... Help Please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

echo and "<!--"... Help Please

Post by zunebuggy »

I have a small PHP script that works great by itself. I have a website that has some javascript, HTML and comments. I converted the html, javascript and comments to echo commands (I've also tried print) but noticed that PHP does not like echo "<!-- where my comments are. As a matter of fact, when it encounters one it turns the rest of my echo statements into comments. When the PHP echos the lines, I would like the <!-- to show up in the final HTML source. There must be an escape character I can put before the <!-- to keep this from happening. Please help.

Thank you
:mrgreen:
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Re: echo and "<!--"... Help Please

Post by zunebuggy »

I also tried removing the comments. That did solved part of the issue, but when I got to

Code: Select all

echo "<SCRIPT LANGUAGE='JavaScript'>";
it handles everything after that strangely. My graphics are now showing up on the page but I am seeing "echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; ...... written in plain text all over my page :(
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: echo and "<!--"... Help Please

Post by semlar »

It doesn't sound like your page is being parsed as php, so it's being displayed as html.
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Re: echo and "<!--"... Help Please

Post by zunebuggy »

I got it. I had my <? in the wrong place and did not notice it until I took a brek and came break with a fresh look at it. 8)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: echo and "<!--"... Help Please

Post by JAB Creations »

1.) There is no "language" attribute in the script element.

2.) Do not use HTML comments to escape JavaScript data, this is a widely common perception.

Here is an example of correctly coded template page...

Code: Select all

<?php
header("Content-type: application/xhtml+xml");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="language" content="" />
<meta name="robots" content="" />
<script type="text/javascript">
//<![CDATA[
function change(id, newClass) {var identity=document.getElementById(id); identity.className=newClass;}
 
function example(event)
{
 if (event < '3') {change('example_id','example_css_over');}
 else if (event > '3') {change('example_id','example_css_out');}
}
//]]>
</script>
<style type="text/css">
div.example_css_over {background-color: #000; color: #fff;}
div.example_css_out {background-color: #fff; color: #000;}
</style>
</head>
 
<body>
 
<h1>Example</h1>
<div id="example_id"><p onmouseover="example('2');" onmouseout="example('4');">Example template</p></div>
 
</body>
</html>
...if you remove the code on 13 and 21 the page will break. You can not test this in Internet Explorer as it does not support XHTML, I recommend testing this with Firefox as if you remove those two lines and execute the page you'll see exactly what I'm talking about. Have fun. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: echo and "<!--"... Help Please

Post by pickle »

JAB Creations wrote:1.) There is no "language" attribute in the script element.
There is if he's using HTML (as opposed to XHTML)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply