Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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>
share|improve this question
up vote 2 down vote accepted

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>
share|improve this answer
    
it still isn't being called up. I am using Wordpress so the variables are being called in from another .php page. Do you think that might be why it is affecting it to not display? – David M. Utt Jul 3 '13 at 20:26
    
What is the HTML output on the page? – M Sost Jul 3 '13 at 20:28
    
its just blank. Like it shows the div around it and just blank space between them – David M. Utt Jul 3 '13 at 20:34
    
can look for yourself chapter.ser.org/midatlantic – David M. Utt Jul 3 '13 at 20:34
    
Please check the edit I just made to my post – M Sost Jul 3 '13 at 20:36

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
}
?> 
share|improve this answer
    
That stopped the breaking but now it doesn't display the link at all, even if there is one set. – David M. Utt Jul 3 '13 at 20:17
    
It's because you have an error in your syntax. There's an extra " after the src. I've removed it in my answer. – M Sost Jul 3 '13 at 20:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.