dqqpf32897 2013-07-03 20:06
浏览 6
已采纳

Php如果是空变量语句

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>
  • 写回答

2条回答 默认 最新

  • doufen1890 2013-07-03 20:11
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题