0

我的数据库中有 5 个字段:

test1 = 1, test2 = 1, test3 = NULL, test4 = NULL, test5 = NULL

PHP代码:

if(isset($result['test1'])){$test1= "Test1"; echo $test1};

if(isset($result['test2'])){$test2= "Test2"; echo $test2};

if(isset($result['test3'])){$test3= "Test3"; echo $test3};

if(isset($result['test4'])){$test4= "Test4"; echo $test4};

if(isset($result['test5'])){$test5= "Test5"; echo $test5};

$total = implode(", ", array_filter(array($test1, $test2, $test3, $test4, $test5)));

echo $total;

最终输出:

Undefined Variable test3 in Line 7

Undefined Variable test4 in Line 7

Undefined Variable test5 in Line 7

测试1,测试2

我想出了 3 种可能的方法来希望使用 NULL 值运行代码,看看我是否会得到一个没有错误的空白页,不幸的是,它们都给了我“Underfined Variable”错误:

  1. if(isset($result['test3'])){$test3= "OK"; echo $test3};

  2. if(!empty($result['test3'])){$test3= "OK"; echo $test3};

  3. if($result['test3']=='1'){$test3= "OK"; echo $test3};

帮助?提前致谢!

4

2 回答 2

2
if(isset($result['test1'])){$test1= "Test1"; echo $test1} else { $test1="Abrakadabra"; }
于 2011-07-03T22:05:13.667 回答
0

A way to do it without array_filter:

 $result = array();

 if(!empty($result['test1'])){$result[] =  "Test1"; echo $test1;}
 ...

 $total = implode(", ", $result);
于 2011-07-03T22:14:27.383 回答