0

I currently have the following code on my page.

<?php 
    if($result["r_approved"] == "APPROVED"){
echo "<!--";
} 
?>
<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="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
            <div id="mid_shdw">
    <?php
    if($result["r_approved"] == "APPROVED"){
echo "-->";
} 
?>

With this code, I'm trying to block/ignore a block of code with <!-- and -->, but it does not want to ignore php code. How would I use PHP to block out an entire section of the code? I would really appreciate if you would use my current "if" statement variables.

5
  • can you just comment out the code you don't want to execute?
    – DrCord
    Dec 3, 2013 at 19:37
  • 1
    Can you be more clear on what code exactly you are trying to ignore? Are you trying to comment the PHP out with <!--?
    – user2816674
    Dec 3, 2013 at 19:38
  • 2
    PHP couldn't care less about HTML comments. If you want to disable PHP code, then you need to use PHP comments: # or /* ... */ or //
    – Marc B
    Dec 3, 2013 at 19:38
  • 1
    Why would you comment it out instead of just not sending it at all?
    – Cyclone
    Dec 3, 2013 at 19:39
  • So this was basically not knowing how to properly use IF statements?
    – LuFaMa
    Dec 3, 2013 at 20:02

3 Answers 3

6

Why not this:

<?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 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">
<?php
}
?>
2
  • 1
    Or even better: <?php if (...): ?> ... <?php endif; ?>. Those lonely closing braces really hurt readability in these situations.
    – Jon
    Dec 3, 2013 at 19:41
  • 1
    Yes, didn't feel like changing more code than needed, especially if they don't use that syntax in other code. Consistency. Dec 3, 2013 at 19:42
1

Put the code inside the if. Please note that I also changed the if to a "does not equal"

<?php if($result["r_approved"] !== "APPROVED"): ?>
    <div class="main">
        ...
    </div>
<?php endif; ?>

I used the alternative syntax for readability reasons as I think it looks much cleaner than with the brackets if you mix PHP with HTML.

0
0

I'd say a much cleaner way to do this would be to use the alternative syntax for the if statement. This removes the obfuscation caused by printing comments from php. In your case, that would be:

<?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 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">
    <?php endif; ?>

You can read more on the alternative syntax here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.