Zen Cart Logo
Forums / General Questions / end tags in PHP comments

end tags in PHP comments

Views: 1,818

Results 1 to 17 of 17
31 May 2012, 10:33 AM
#1
andriks avatar

andriks

New Zenner

Join Date:
May 2012
Location:
Delft, the Netherlands
Posts:
7
Plugin Contributions:
0

end tags in PHP comments

Hi all,

In many (!) files I encounter php end tags (?>) after the start of a single line comment (//). On many occasions this is not a problem in practice, because the first statement after that is a php open tag (<?php) which is ignored by php. But in at least one case I could trace curious output back to such a missed php end. Are you aware of this problem? I am using PHP 5.3.8.

Yours,

andriks

3 Jun 2012, 8:50 AM
#4
andriks avatar

andriks

New Zenner

Join Date:
May 2012
Location:
Delft, the Netherlands
Posts:
7
Plugin Contributions:
0

Re: end tags in PHP comments

gjh42:

The comment marks // do not hide the PHP end tag from processing.

Oh yes they do. At least in my configuration. Try the following:
<html>

<head> <title>PHP test page</title> </head> <body> <?php // the first line is always printed: echo "<h1>Welcome to the PHP test page</h1>"; // PHP ends? ?> echo "<p>What is on this line?</p>"; <?php // if PHP did not end the previous line will only contain the paragraph, not the whole echo. echo "<p>Back in PHP?</p>"; ?> <?php // the next line is always printed: echo "<h2>Another PHP test</h2>"; // PHP ends? ?> <?php echo "<p>Because we switch back to PHP immediately after the missed end tag, we are okay here.</p>"; ?> <?php phpinfo(); ?> </body> </html>

Notice how the second echo is displayed!

Yours,

Andriks

3 Jun 2012, 12:00 PM
#6
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: end tags in PHP comments

Those comments will stop all the php on that line after them being processed.

It is just very bad coding. And I should mention, it does not occur in the default Zen Cart files. This line:

// PHP ends? ?>

Should be:

 
// PHP ends?
?>

So, either there has been a problem in uploading files where the line-breaks have got messed up for whatever reason. Or the code is just plain wrong. IMO.

Either way it needs to sorted out. I am guessing that the code in question comes from a template or a module rather than the core Zen Cart files?

Nick
iszent.com

3 Jun 2012, 1:23 PM
#7
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: end tags in PHP comments

Yes, testing in actual PHP/HTML output does behave as you described. This is apparently a bug in Notepad++, as it shows that the trailing ?> is active and the following line is outside PHP.

3 Jun 2012, 2:02 PM
#8
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: end tags in PHP comments

Very weird... if you look closely at the output of the test page, you will see (at least I do) that the output is not proper HTML: the <h1> is not in h1 formatting, and the closing quote and end-of-line semicolon print when they should not. Duplicating the first echo line results in the second echo being formatted as an h1 heading.

There is lots of stuff going on that should not be happening with PHP to HTML output... don't have time to elaborate at the moment, have to go off to breakfast.

Welcome to the PHP test page'; echo "
Welcome to the PHP test page
"; // PHP ends? ?> echo "

What is on this line?
"; Back in PHP?

"; ?> Another PHP test"; // PHP ends? ?> Because we switch back to PHP immediately after the missed end tag, we are okay here.

"; ?>

3 Jun 2012, 2:35 PM
#9
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: end tags in PHP comments

Not really all that weird. PHP manual says:

The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that. If the asp_tags configuration directive is enabled, it behaves the same with // %> and # %>. However, the </script> tag doesn't break out of PHP mode in a one-line comment.

The closing quote and semi-colon print because those are not code in php mode.

So, I have to hold my hand up and say my previous comment was inaccurate. But when I run your code I see exactly what I would expect output to the page. And I'd add that it is pretty common practice to comment either on separate lines or after the end of a line of code.

The source code I get when I run that is:

<html>
<head>
<title>PHP test page</title>
</head>
<body>
<h1>Welcome to the PHP test page</h1>echo "<p>What is on this line?</p>";
<p>Back in PHP?</p><h2>Another PHP test</h2><p>Because we switch back to PHP immediately after the missed end tag, we are okay here.</p></body>
</html>

Which seems completely right. I exclude the php info section as it just introduces two <html> tags to the document

Nick
iszent.com

3 Jun 2012, 5:02 PM
#10
andriks avatar

andriks

New Zenner

Join Date:
May 2012
Location:
Delft, the Netherlands
Posts:
7
Plugin Contributions:
0

Re: end tags in PHP comments

niccol:

Those comments will stop all the php on that line after them being processed.

It is just very bad coding. And I should mention, it does not occur in the default Zen Cart files.

It does occur in the template_default files. The results of a simple scan on the occurrence of ?> after // yields:

includes\templates\template_default\templates\tpl_account_default.php
warning: PHP end tag found in comment on line 74 !
warning: PHP end tag found in comment on line 80 !
warning: PHP end tag found in comment on line 83 !
97 lines.

includes\templates\template_default\templates\tpl_account_history_info_default.php
warning: PHP end tag found in comment on line 135 !
150 lines.

includes\templates\template_default\templates\tpl_checkout_confirmation_default.php
warning: PHP end tag found in comment on line 111 !
warning: PHP end tag found in comment on line 112 !
warning: PHP end tag found in comment on line 129 !
warning: PHP end tag found in comment on line 149 !
warning: PHP end tag found in comment on line 153 !
warning: PHP end tag found in comment on line 160 !
184 lines.

includes\templates\template_default\templates\tpl_checkout_payment_default.php
warning: PHP end tag found in comment on line 41 !
warning: PHP end tag found in comment on line 55 !
warning: PHP end tag found in comment on line 99 !
warning: PHP end tag found in comment on line 200 !
210 lines.

includes\templates\template_default\templates\tpl_document_general_info_display.php
warning: PHP end tag found in comment on line 111 !
warning: PHP end tag found in comment on line 112 !
228 lines.

includes\templates\template_default\templates\tpl_document_product_info_display.php
warning: PHP end tag found in comment on line 111 !
warning: PHP end tag found in comment on line 112 !
229 lines.

includes\templates\template_default\templates\tpl_ezpages_bar_footer.php
warning: PHP end tag found in comment on line 23 !
24 lines.

includes\templates\template_default\templates\tpl_ezpages_bar_header.php
warning: PHP end tag found in comment on line 23 !
25 lines.

includes\templates\template_default\templates\tpl_featured_products_default.php
warning: PHP end tag found in comment on line 82 !
83 lines.

includes\templates\template_default\templates\tpl_index_categories.php
warning: PHP end tag found in comment on line 63 !
118 lines.

includes\templates\template_default\templates\tpl_index_product_list.php
warning: PHP end tag found in comment on line 35 !
warning: PHP end tag found in comment on line 154 !
211 lines.

includes\templates\template_default\templates\tpl_login_default.php
warning: PHP end tag found in comment on line 23 !
warning: PHP end tag found in comment on line 31 !
100 lines.

includes\templates\template_default\templates\tpl_modules_attributes.php
warning: PHP end tag found in comment on line 17 !
67 lines.

includes\templates\template_default\templates\tpl_modules_listing_display_order.php
warning: PHP end tag found in comment on line 27 !
35 lines.

includes\templates\template_default\templates\tpl_modules_products_quantity_discounts.php
warning: PHP end tag found in comment on line 82 !
83 lines.

includes\templates\template_default\templates\tpl_page_not_found_default.php
warning: PHP end tag found in comment on line 39 !
55 lines.

includes\templates\template_default\templates\tpl_products_all_default.php
warning: PHP end tag found in comment on line 84 !
85 lines.

includes\templates\template_default\templates\tpl_products_new_default.php
warning: PHP end tag found in comment on line 83 !
84 lines.

includes\templates\template_default\templates\tpl_product_free_shipping_info_display.php
warning: PHP end tag found in comment on line 111 !
warning: PHP end tag found in comment on line 112 !
229 lines.

includes\templates\template_default\templates\tpl_product_info_display.php
warning: PHP end tag found in comment on line 112 !
warning: PHP end tag found in comment on line 113 !
230 lines.

includes\templates\template_default\templates\tpl_product_music_info_display.php
warning: PHP end tag found in comment on line 111 !
warning: PHP end tag found in comment on line 112 !
241 lines.

includes\templates\template_default\templates\tpl_shopping_cart_default.php
warning: PHP end tag found in comment on line 49 !
warning: PHP end tag found in comment on line 50 !
232 lines.

includes\templates\template_default\templates\tpl_site_map_default.php
warning: PHP end tag found in comment on line 42 !
88 lines.

Yours,

Andriks

3 Jun 2012, 5:17 PM
#11
andriks avatar

andriks

New Zenner

Join Date:
May 2012
Location:
Delft, the Netherlands
Posts:
7
Plugin Contributions:
0

Re: end tags in PHP comments

Should have consulted the real PHP manual. The book I'm using states that the // comment terminates all parsing for that line. And I did notice some strange things related to a missed ?>, but I have to admit that was in a template I downloaded.

Case closed.

Andriks

4 Jun 2012, 5:00 AM
#12
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: end tags in PHP comments

?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that. That was my understanding, and Notepad++ was not being buggy when it showed that kind of syntax highlighting.

This PHP ```php
...

<body> <?php // the first line is always printed: echo '<h1>Welcome to the PHP test page</h1>'; echo "<h1>Welcome to the PHP test page</h1>"; // PHP ends? ?> echo "<p>What is on this line?</p>"; ``` displays as > Welcome to the PHP test page'; echo " > **Welcome to the PHP test page** > "; // PHP ends? ?>echo " > > What is on this line? > "; The output of this code is puzzling - it seems as if the first ' is ignored and following ones are swapping the PHP and non-PHP parts...
4 Jun 2012, 6:50 AM
#13
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: end tags in PHP comments

Glenn,

For me the code

<?php // the first line is always printed:
echo '<h1>Welcome to the PHP test page</h1>';
echo "<h1>Welcome to the PHP test page</h1>";
// PHP ends? ?>

Outputs source:

<body>
<h1>Welcome to the PHP test page</h1><h1>Welcome to the PHP test page</h1>echo "<p>What is on this line?</p>";

Nick
iszent.com

4 Jun 2012, 7:11 AM
#14
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: end tags in PHP comments

I abbreviated the code I was testing for simplicity; it was the whole file posted previously (post 4) with the Welcome line duplicated and single-quoted instead of double-quoted. View source shows the entire file including all PHP markup and comments. The display begins as I noted above.
What code were you using to get the source/display you posted?
Should there be any difference in a XAMPP localhost output vs. a remote server's output for this? I wouldn't think so...

4 Jun 2012, 8:06 AM
#15
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: end tags in PHP comments

Are you using the php info line? As I said before it is going to screw up display as it introduces multiple html tags into the document. Other than that I don't really know. My code is :

<html>
<head>
<title>PHP test page</title>
</head>
<body>
<?php // the first line is always printed:
echo '<h1>Welcome to the PHP test page</h1>';
echo "<h1>Welcome to the PHP test page</h1>";
// PHP ends? ?>
echo "<p>What is on this line?</p>";
<?php // if PHP did not end the previous line will only contain the paragraph, not the whole echo.
echo "<p>Back in PHP?</p>";
?>
<?php // the next line is always printed:
echo "<h2>Another PHP test</h2>";
// PHP ends? ?>
<?php echo "<p>Because we switch back to PHP immediately after the missed end tag, we are okay here.</p>"; ?>
</body>
</html>

Output is:

<html>
<head>
<title>PHP test page</title>
</head>
<body>
<h1>Welcome to the PHP test page</h1><h1>Welcome to the PHP test page</h1>echo "<p>What is on this line?</p>";
<p>Back in PHP?</p><h2>Another PHP test</h2><p>Because we switch back to PHP immediately after the missed end tag, we are okay here.</p></body>
</html>

Nick
iszent.com

4 Jun 2012, 8:46 AM
#16
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: end tags in PHP comments

Moderator note: This thread has been moved OUT of the "Bug Reports" section because it is not a bug with Zen Cart code.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

4 Jun 2012, 3:02 PM
#17
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: end tags in PHP comments

Weird display mystery solved. My computer was displaying the test file as a file through its physical filepath, not through localhost. When I switched the address, it displayed as it ought to.