David M. Utt
David M. Utt

Reputation: 11

Php if empty variable statement

I'm trying to build an if statement for when a variable is filled with a url it will display a button and link to the site, and when the variable is empty the link and button will not display. So far I got it to where it is displays the button and links but making it into an if statement keeps on breaking my site. If you see a problem with the code below, please help. Thanks

<div id="social_icon">
<?php if (isset($fburl))
{ 
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"    
height="30"></a>; 
}
else
{
//dont show anything
}
?>   
</div>

Upvotes: 0

Views: 1247

Answers (2)

M Sost
M Sost

Reputation: 1153

You're trying to use HTML within your PHP code, so PHP sees this as an unexpected variable/string. Either use echo for this, or close the PHP statement, and then write your HTML.

Either:

<div id="social_icon">
    <?php if(isset($fburl)){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>

Or:

<div id="social_icon">
    <?php if (isset($fburl)){ 
        echo '<a href="'.$options['fburl'].'"><img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" /></a>'; 
    }else{
        //dont show anything
    } ?>   
</div>

Edit

Actually, I would assume it's not outputting anything because your if statement is checking for $fburl whereas you're echoing the link as $options['fburl']. If the facebook url is located at $options['fburl'], try:

<div id="social_icon">
    <?php if(isset($options['fburl'])){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>

Edit 2

If the options are set but don't contain a link, you will also need check for that:

<div id="social_icon">
    <?php if(isset($options['fburl']) && !empty($options['fburl'])){ ?> 
        <a href="<?php echo $options['fburl']; ?>">
            <img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
        </a>
    <?php }else{
        //dont show anything
    } ?>   
</div>

Upvotes: 2

user1646111
user1646111

Reputation:

Syntax error, change it to:

<?php if (isset($fburl))
{
//missed end tag here
?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"    
height="30"></a>; 
<?php
//add another php start tag
}
else
{
//dont show anything
}
?> 

Upvotes: 0

Related Questions