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条)

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆