PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] display none if php function is empty

 February 17, 2022     php, string, wordpress     No comments   

Issue

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;

I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.

<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>

Solution

Why not completely not echo "advert" element?

   if ( !empty($_GET['details']) ){
       echo '<div class="advert">add text</div>';
    }

if you really want to just hide, you can assign hide class

<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>

then you would need to add "hide" class with display:none in your style.css

Above is shorthand/ternary if/else statement used, its great if you need output some string.

And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.

Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().



Answered By - Maximus Light
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing