Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a Div using php

I am currently hiding a div based on an if statement. The method I use is, use echo out a css style of display: none

Here is what I am doing specifically:

<style>
  #content{
    <?php
      if(condition){
          echo 'display:none';
      }
    ?>
  }
</style>
<body>
    <div id="content">
       Foo bar
    </div>
</body>

My question being, Is this a good method for a hiding a div? Is it possible that browser cache the style and therefore ignore the echo-ed out css style?

like image 822
Krimson Avatar asked Aug 07 '12 23:08

Krimson


3 Answers

Using Php in your CSS (Cascade Style Sheet) is not "proper",

Also, You can use Php in your HTML:

<body>
    <?php if (condition){ ?>
        <div id="content">
           Foo bar
        </div>
    <?php } ?>
</body>

With this code, div block don't appear, (and you don't use it with JavaScript), You can use this for just hidden your div :

 <body>
    <div id="content" <?php if (condition){ echo 'style="display:none;"'; } ?>>
       Foo bar
    </div>
</body>
like image 79
Doc Roms Avatar answered Sep 20 '22 02:09

Doc Roms


Why not create a class:

<style>
    .hidden {
        display: none;
    }
</style>

And than apply it with PHP:

<div id="content" <?php print ( condition ? 'class="hidden"' : '' ); ?> >
like image 39
insertusernamehere Avatar answered Sep 22 '22 02:09

insertusernamehere


That would not be the best way to hide a div. Since PHP is parsed server-side, you might as well have the if statement include or exclude the div instead of echoing a CSS class. The only time that would be useful with a CSS class is if you plan to use JavaScript to show the div on the page later on, while the user is on the page itself.

like image 32
Kyle Ross Avatar answered Sep 20 '22 02:09

Kyle Ross