not null 不使用 php (wordpress)
not null not working with php (wordpress)
如果变量不为空,我想在 wordpress 中显示一个标签。但是即使变量为空,也会执行。这是我的变量:
<?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';
?>
执行位置:
<?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
}
?>
这也会执行最后一个变量 $tr_vo1_f6
。 if 语句似乎是问题所在,但我无法弄清楚如何以不同的方式编写它们,除了 if (!($var == NULL))
从我读过的内容来看,它与 if($var)
.
相同
如何正确编写 if 语句?
您分配的是一个字符串,而不是真正的 null
值。你应该修复:
<?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;
?>
您可以使用 isse() 来确定变量是否不为 NULL。
http://php.net/manual/en/function.isset.php
示例:
if (isset($st_tr))
....
不要使用引号分配空值
$st_tr = get_field('startkostnad_transfertryck') ?: 'null';
如果使用单引号,则赋值为字符串,且字符串不为空
正确的方法是:
$st_tr = get_field('startkostnad_transfertryck') ?: null;
如果变量不为空,我想在 wordpress 中显示一个标签。但是即使变量为空,也会执行。这是我的变量:
<?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';
?>
执行位置:
<?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
}
?>
这也会执行最后一个变量 $tr_vo1_f6
。 if 语句似乎是问题所在,但我无法弄清楚如何以不同的方式编写它们,除了 if (!($var == NULL))
从我读过的内容来看,它与 if($var)
.
如何正确编写 if 语句?
您分配的是一个字符串,而不是真正的 null
值。你应该修复:
<?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;
?>
您可以使用 isse() 来确定变量是否不为 NULL。
http://php.net/manual/en/function.isset.php
示例:
if (isset($st_tr))
....
不要使用引号分配空值
$st_tr = get_field('startkostnad_transfertryck') ?: 'null';
如果使用单引号,则赋值为字符串,且字符串不为空
正确的方法是:
$st_tr = get_field('startkostnad_transfertryck') ?: null;