3

As a learner in PHP, I'm struggling to hide from displaying several lines, if a chosen variable is empty. I can get this to work on a basic level, but am lost when the content to be hidden gets more complicated. The concept I am aiming at is this:

<?php if (isset($g1)) { ?>
((This part should not display if variable $g1 is empty))
<?php } ?>

My code looks like this:

<?php if (isset($g1)) { ?>
<a href="img/g1.png"  class="lightbox"  rel="tooltip" data-original-title="<?php print $g1; ?>" data-plugin-options='{"type":"image"}'>
<img class="img-responsive img-rounded wbdr4" src="img/g1.png">
</a>
<?php } ?>

In the above, the tooltip does not display when variable g1 is empty, but the rest does. I'm new here so, I hope I've formatted my question correctly. Help appreciated.

5
  • It looks like they are using that.. But their coding style is very unreadable in my eyes. Commented Jul 21, 2014 at 4:13
  • Use if ($g1 != NULL) { //img tag } Commented Jul 21, 2014 at 4:15
  • I suggest taking a look at my answer first, cause every single answer here misunderstood your question. (OR I am the idoit and everyone else is right :P ) Commented Jul 21, 2014 at 4:26
  • u can use the empty() function.it will handle all. php.net/manual/en/function.empty.php Commented Jul 21, 2014 at 4:39
  • Just a note to say that I appreciate the help given. This is an amazing community! :-) Commented Jul 21, 2014 at 15:56

6 Answers 6

2
<?php if (isset($g1) && $g1!='') { ?>
<a href="img/g1.png"  class="lightbox"  rel="tooltip" data-original-title="<?php print $g1; ?>" data-plugin-options='{"type":"image"}'>
<img class="img-responsive img-rounded wbdr4" src="img/g1.png">
</a>
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

2
Try this code:
<?php if (!empty($g1)) { ?>
((This part should not display if variable $g1 is empty))
<?php } ?>

Comments

0

The isset() function checks if the variable has been set, even if to an empty string. There's the empty() function which checks if the variable is not set or set to empty string.

<?php
$x = '';
if (isset($x)) print('$x is set');
if (empty($x)) print('$x is not set or is empty');
if (isset($x) && empty($x)) print('$x is set and is empty');
if (!empty($x)) print('$x is set and not empty'); // won't emit warning if not set

Comments

0

You can make use if empty() function:

<?php if (isset($g1) && !empty($g1)) { ?>
((This part should not display if variable $g1 is empty))
<?php } ?>

or

<?php if (isset($g1) && $g1 !== '') { ?>
((This part should not display if variable $g1 is empty))
<?php } ?>

3 Comments

if its not empty, then surely it must be set
isset just checks for null, you can still check for empty strings with empty()
No need to. Just like isset(), empty() won't issue Warnings when used with undeclared variables, and should return FALSE if it's undeclared.
0

Here's my answer that seems to be going a totally different route than everyone else, which i have no idea why.

To do what OP wants, one way is to expand the php tag to include all.

<?php 
    if (isset($g1)) { 
        echo "<a href='img/g1.png'  class='lightbox'  rel='tooltip' data-original-title='".$g1."' data-plugin-options='{\"type\":\"image\"}'>";
        echo "<img class='img-responsive img-rounded wbdr4' src='img/g1.png'>";
        echo "</a>";
    } 
?>

Comments

-2

hiding something is pretty easy in php you can use it in several ways and here is how it would look like

<?php if(isset($g1) == ""): ?>
   //The $g1 is empty so anything here will be displayed
<?php else: ?>
   //The $g1 is NOT empty and anything here will be displayed
<?php endif; ?>

1 Comment

isset($g1) == ""... really?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.