3

I have the following problem. I used the following code in my page to ignore some php code, but it seems that over the Thanksgiving weekend there was an update and it is no longer ignoring the code.

<!--

<div class="main">
    <div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); 
?>
<div id="mid-top"><img src="" width="990" height="20" alt="Top Spacer"/></div>
            <div id="mid_shdw">

-->

The rest of the html code is being ignored, but only php code is not being ignored. I know one of the ways is to include <!-- into the php function. But is there any other way to ignore the php code with the rest of the html code?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Leo Vorontsov
  • 83
  • 1
  • 1
  • 3
  • 5
    PHP is run on the server side and it doesn't know what HTML comments are. Everything between `` will be exectued. Only the browser won't display what's between ``. – Felix Kling Dec 02 '13 at 22:34
  • 2
    Who in the world upvoted this 4x?? – Funk Forty Niner Dec 02 '13 at 23:00
  • ....I stand corrected: `6x` - Obviously 6 ghost accounts. – Funk Forty Niner Dec 02 '13 at 23:08
  • 1
    @Fred-ii- - while this might be a simple question to some, you gotta admit it's quite a lot better than a lot of the posts going through the PHP tag :P – Lix Dec 02 '13 at 23:10
  • 1
    I must admit, there have been quite a few "real stupid" questions related to DB lately. I guess you have a point there. Bizarre though as to why the OP's code stops all of a sudden, however I've never in my entire experiences ever seen `` ever worked in PHP. It's like you said in your answer, they are HTML comments, *pure & simple.* @Lix – Funk Forty Niner Dec 02 '13 at 23:15
  • @Fred-ii- - It might be related to the short tags ` ?>` - maybe the hosting service turned them off/on or something... That's the only other thing I can think of... – Lix Dec 02 '13 at 23:16
  • I have a feeling you may be right, as bizarre as it seems. @Lix – Funk Forty Niner Dec 02 '13 at 23:19

7 Answers7

13

This is an HTML comment. It has no effect on the PHP code.

You should use PHP comments:

Block comment:

/*
BLOCK OF COMMENTED CODE
*/

Line comment:

// this is a commented line

The PHP code is interpreted by the server and is calculated "long" before it gets to the users browser. The HTML markup while still on the server, is just text. Only when the HTML arrives at the users browser does it get rendered (or ignored!). So your HTML comments did not matter to the server - it saw PHP code and ran it - the PHP interpreter is not programmed to recognize these strange <!-- symbols that you are giving it. ;)

Lix
  • 47,311
  • 12
  • 103
  • 131
5

Your PHP code will always be executed because it doesn't know about your HTML code that surrounds it.

The solution, if you your PHP code not to execute is to comment it out:

 <!--

<div class="main">
  <div class="main-sub">
<?
// php include('http://www.contractorsintelligence.com/contractors-license/includes- 
// page-elements/navigation1.php'); 
?>
<div id="mid-top"><img src="" width="990" height="20" alt="Top Spacer"/></div>
        <div id="mid_shdw">
-->
Lix
  • 47,311
  • 12
  • 103
  • 131
SkaiBoa
  • 53
  • 7
4

If you want to ignore the PHP code, its your best bet to do it like this:

<?php 
    /* include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); */
?>

Whereas /* starts a comment and */ ends it.

PHP will parse the page before it is sent to the client (or browser). Therefore PHP is not 'interested' in <!-- or --> at all.

On the other hand, if the HTML code that is being included by your call to include() contains further HTML commentary (<!-- or -->) it may close your ignored code before the point you intended it to.

UPDATE

Your overall approach is a bit fuzzy. See here, if you want to use PHP to decide whether to show certain HTML code or not, you don't want to use HTML comments to accomplish that.

Try this instead:

<?php 
    if($result["r_approved"] != "APPROVED"){
?>
<div class="main">
    <div class="main-sub">
        <?php 
            include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); 
        ?>
    </div>
    <div id="mid-top">
        <img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/>
    </div>
    <div id="mid_shdw"></div>
</div>
<?php
    }
?>
SquareCat
  • 5,699
  • 9
  • 41
  • 75
4
<?php /* comments */ ?>

The PHP is executed before the HTML is processed client-side.

Matt
  • 22,721
  • 17
  • 71
  • 112
2

You php page is executed and everything between <? ?> is executed. Php doesn't care about <!-- --> or any other tag except <? or <?php .

Then the browser doesn't display/load what is inside <!-- -->.

If you want to comment php, use // or /* ... */

<?php /* include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); */ ?>
kmas
  • 6,401
  • 13
  • 40
  • 62
1

Two things are happening at once which I think might be confusing:

  • Unless you wrap everything inside the php tags with /* */ or use // that code will be executed because it comes from the server.

  • The browser is the only one that parses the <!-- -->.

So your server is parsing the php and then the browser is hiding what was parsed.

Solution

<?php // include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>

Michael J. Calkins
  • 32,082
  • 15
  • 62
  • 91
1

Thats because the <!-- isn't parsed by PHP, only by the browser. The easiest (but not always best readable) solution is

<?php if (false) { ?>
<b>This html will not be sent to browser</b>
<?php include('this will not be included'); ?>
<?php } // endif ?>
Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
  • Your example shows HTML not being parsed - but the question itself is about the PHP code. – Lix Dec 02 '13 at 22:45
  • I think I showed both (`include('this will not be included')`) – Peter van der Wal Dec 02 '13 at 22:46
  • Yes - but you are using a conditional statement. Ignoring a line of code doesn't entail a condition. At least that's how I understood the question. The OP is looking for ways to "comment out" or ignore certain lines. The confusion comes when mixing languages in the same file. – Lix Dec 02 '13 at 22:48
  • He asked for `ignore the php code with the rest`. I agree this will not totally ignore the code, but sometimes adding a `/* ... */` to a well-documented part of code (thus containing `*/` itself) isn't an option so I provided another way. – Peter van der Wal Dec 02 '13 at 22:53
  • 1
    Ohh... I see what you mean... You might want to look into [Heredocs](http://stackoverflow.com/questions/5673269/is-there-a-reason-to-use-heredoc-in-php) for that. It involves less code manipulation so to comment something out it would work quite well :) – Lix Dec 02 '13 at 22:57
  • Sweet solution, that Heredocs. Never thought of using those in that situation. – Peter van der Wal Dec 02 '13 at 22:59
  • It's really ugly, so my advice would be to stay away from it completely... But for cases where you are debugging and need to temporarily bypass things - it works well. – Lix Dec 02 '13 at 23:01