douhuiwan5141 2017-03-08 15:27
浏览 44
已采纳

not null不使用php(wordpress)

I would like to display a tag in wordpress if a variable isn't null. But the executes even if the variable is null. Here are my variables:

<?php
$st_tr = get_field('startkostnad_transfertryck') ?: 'null';

$tr_vo1_f1 = get_field('tr_vo1_f1') ?: 'null';
$tr_vo1_f2 = get_field('tr_vo1_f2') ?: 'null';
$tr_vo1_f3 = get_field('tr_vo1_f3') ?: 'null';
$tr_vo1_f4 = get_field('tr_vo1_f4') ?: 'null';
$tr_vo1_f5 = get_field('tr_vo1_f5') ?: 'null';
$tr_vo1_f6 = get_field('tr_vo1_f6') ?: 'null';
?>

And where it's executed:

<?php    
if ($st_tr) {
    echo $st_tr;    
?>
<select name="print" id="print_m">
    <option value="0">Ingen märkning</option>
    <?php
    // Color quantities   
    $c_q = array("$tr_vo1_f1", "$tr_vo1_f2", "$tr_vo1_f3", "$tr_vo1_f4", "$tr_vo1_f5", "$tr_vo1_f6");
    // not null
    $c_q_nn = array_filter($c_q, 'strlen');

    // Color quantity and display (check if exists)
    if ($tr_vo1_f1){    
       $c_q_d_f1 = "1-färgstryck";  
    }  
    if ($tr_vo1_f2){
       $c_q_d_f2 = "2-färgstryck";  
    }  
    if ($tr_vo1_f3){
       $c_q_d_f3 = "3-färgstryck";  
    }   
    if ($tr_vo1_f4){
       $c_q_d_f4 = "4-färgstryck";  
    }  
    if ($tr_vo1_f5){
       $c_q_d_f5 = "5-färgstryck";  
    }  
    if ($tr_vo1_f6){
       $c_q_d_f6 = "6-färgstryck";  
    }      
    $c_q_d = array("$c_q_d_f1", "$c_q_d_f2", "$c_q_d_f3", "$c_q_d_f4", "$c_q_d_f5", "$c_q_d_f6");
    $c_q_d_nn = array_filter($c_q_d, 'strlen');
    foreach (array_combine($c_q_nn, $c_q_d_nn) as $color_q => $color_q_d) {    
        echo '<option value="' . $color_q . '">' . $color_q_d . '</option>';     
    }

    ?>    
</select>    
<?php 
}
?>

This also executes the last variable $tr_vo1_f6. The if statements seems to be the problem but I can't figure out how to write them differently except if (!($var == NULL)) which from what I've read would be the same as if($var).

How would I write the if statements correctly?

  • 写回答

2条回答 默认 最新

  • douhuan5073 2017-03-08 15:34
    关注

    You are assigning a string and not a really null value. You should fix with:

    <?php
    $st_tr = get_field('startkostnad_transfertryck') ?: null;
    
    $tr_vo1_f1 = get_field('tr_vo1_f1') ?: null;
    $tr_vo1_f2 = get_field('tr_vo1_f2') ?: null;
    $tr_vo1_f3 = get_field('tr_vo1_f3') ?: null;
    $tr_vo1_f4 = get_field('tr_vo1_f4') ?: null;
    $tr_vo1_f5 = get_field('tr_vo1_f5') ?: null;
    $tr_vo1_f6 = get_field('tr_vo1_f6') ?: null;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分