Hiding a Div using php

2019-05-02 18:35发布

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?

6条回答
贪生不怕死
2楼-- · 2019-05-02 19:09

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>
查看更多
在下西门庆
3楼-- · 2019-05-02 19:10

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.

查看更多
爷的心禁止访问
4楼-- · 2019-05-02 19:16

You could:

<div runat="server" id="theDiv"> 

Code behind is
{
theDiv.Visible = False;
}

or

Most people would use javascript

Heres a previous thread that will help you out:

Javascript if else statement to hide and show div

查看更多
太酷不给撩
5楼-- · 2019-05-02 19:17

Yes, that is the standard way of hiding a div. With regard to the browser cache, it shouldn't cache this as it isn't in an external stylesheet.

查看更多
太酷不给撩
6楼-- · 2019-05-02 19:26

I generally try to avoid using PHP Conditionals inside of CSS; especially inline CSS (CSS that is on the same page).

I would keep your CSS in its own CSS file and use the PHP conditional to either add a "hide" class to the DIV -OR- not echo the DIV at all.

<link rel="stylesheet" type="text/css" href="style.css" />
<body>
    <div id="content" <?php if(conditional) : ?>class="hide"<?php endif;?>>
       Foo bar
    </div>
</body>

or alternatively

<?php $class = (conditional) ? "hide" : ""; ?>

<link rel="stylesheet" type="text/css" href="style.css" />
<body>

    <div id="content" class="<?=$class?>">
       Foo bar
    </div>
</body>

or

<link rel="stylesheet" type="text/css" href="style.css" />
<body>

    <?php if (conditional) : ?>
    <div id="content">
       Foo bar
    </div>
    <?php endif; ?>
</body>

Many times the div needs to be outputted so it can be re-displayed using JavaScript (e.g. carousels, sliders, etc.)

查看更多
7楼-- · 2019-05-02 19:29

Why not create a class:

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

And than apply it with PHP:

<div id="content" <?php print ( condition ? 'class="hidden"' : '' ); ?> >
查看更多
登录 后发表回答