1

Anything wrong with this code, it works great, but I don't understand the forth line. Why is closing bracket all by itself? I am a fairly new to PHP and always Google for answers, but I cant figure this one out. Hopefully, I can help others someday. Thanks

<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>      
    <?php } ?>
</div>  
Medeno
  • 173
  • 1
  • 11
  • This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing – Exwolf Aug 31 '13 at 01:09

4 Answers4

0

Nothing is wrong with it. You can break in and out of PHP, and that is what this code is doing. Sometimes it's easier to break out of a PHP block to write some HTML, then go back into PHP

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
0

It's ending the if statement created on line 2, but line 3 is outputting HTML so php is ended, only to begin on the next line to finish the open statement.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
0

Write it like this you suddenly know:

<div class="errorbox">
<?php 

 if(isset($error2)) {
     echo '<strong class="error">' . $error2 . '</strong>';
  }

?>
</div> 

Or like so:

<div class="errorbox">
<?php 

  if(isset($error2)) {

?>
<strong class="error"><?php echo $error2;?></strong>      
<?php 

   } 

?>
</div> 
SteAp
  • 11,853
  • 10
  • 53
  • 88
0

This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing

There are several ways of doing it:

The best way is as descibed in the question:

<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>      
    <?php } ?>
</div>  

Another way is by echoing:

echo "<div class="errorbox">";

    <?php
     if(isset($error2)){
    echo "<strong class="error">". $error ."</strong>";
    }
    ?>      
    echo "</div>";
Exwolf
  • 157
  • 1
  • 12