awkwrd
awkwrd

Reputation: 43

PHP - if html commenting

I would like to hide part of a pages content depending on the url.

at the moment if I typed:

example.co.uk/index.php?brand=apple

then the apple content will show - however there is some default div's on the page that I'd like to hide if no brand was shown.

I was thinking along the lines of:

    <?php if brand==""
echo "<!----- comment out all the default HTML"
/>

default html here

<?php if brand==""
echo "-->"
/>

Would that be the correct way to go about clearing the page if no brand was mentioned in the url?

Upvotes: 0

Views: 123

Answers (5)

Quentin
Quentin

Reputation: 944158

No, the bit of comment syntax that means "start of comment" and "end of comment" in HTML 4 and XHTML is -- so your syntax has a closed comment on line 1.

You want <!--.

This will break if the HTML contains comments though.

You also need some () for the if.

You are probably best off not including the HTML in the output at all if the match matches.

<?php 
    if (brand != "") {
?>
HTML here
<?php
    }
?>

Upvotes: 0

Helmut
Helmut

Reputation: 1377

You need to be aware, that even if you "hide" your html with comments, it is not really hidden, as first it is still visible in the source code and secondly, you can easily get into troubles, e.g.:

you have one comment <!-- text 1 and another comment inside this <!-- text 2 --> and then want to close the first comment -->.

The closing of text 2 will actually close text 1, etc. So it is really not a clean solution. If you work with PHP, it would be best practice to just print this code in your html, that you DO want to show.

If you have $_GET['brand'] then use if or switch to just echo the content you'd like to show.

EDIT: it seems, that you only want to show some elements, if $_GET['brand'] is not emtpy, right? In that case:

if(isset($_GET['brand'])) {
    // echo your html here...
}

Upvotes: 1

Peon
Peon

Reputation: 8030

Don't hide what you don't need, show only, what you need:

<?php

switch ( $type ) {

case "apple":
  // apple content
break;

case "peaches":
    // peaches content
break;

...


}

?>

Upvotes: 0

deceze
deceze

Reputation: 522500

You don't need to comment out the HTML, you simply need to not output the HTML you don't want shown.

<?php if (/* something */) : ?>

    Output HTML for something.

<?php endif; ?>

The "HTML for something" will not be output unless the condition is true...

Upvotes: 3

Ben
Ben

Reputation: 57287

You don't even need the comments; just the PHP if block will be fine:

<?php if ($brand) { ?>

...html - note this section is bounded by PHP, but is not PHP code...

<?php } ?>

Alternatively, store your "default html" in a string, and echo it:

<?php
if ($brand) {
  echo $strHTML;
}
?>

Upvotes: 0

Related Questions