PHP - If variable is not empty, echo some html cod

2020-02-07 17:48发布

I would like to display some html code if a variable is not empty, else I would like to display nothing.

I've tried this code but doesn't work:

<?php 
    $web = the_field('website');
    if (isset($web)) {
?>
       <span class="field-label">Website: </span><a href="http://<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a> 
<?php
    } else { 
        echo "Niente";
    }
?>

10条回答
【Aperson】
2楼-- · 2020-02-07 18:38
if(!empty($web))
{
   echo 'Something';
}
查看更多
祖国的老花朵
3楼-- · 2020-02-07 18:41
if($var !== '' && $var !== NULL)
{
   echo $var;
}
查看更多
够拽才男人
4楼-- · 2020-02-07 18:46
if (!empty($web)) {
?>
    <span class="field-label">Website:  </span><a href="http://<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a> 
<?php
} else { echo "Niente";}

http://us.php.net/manual/en/function.empty.php

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-07 18:52

Seems like folks are complicating this a bit. Back to the original question, " ... if variable is not empty, echo some html code." "I would like to display some html code if a variable is not empty, else I would like to display nothing."

Easy way:

<?php if (!empty($var)) echo "Some Html Code Here"; ?>

If your variable is not empty "Some HTML code here" will be displayed. If it is empty nothing will happen.

查看更多
登录 后发表回答