PHP 中未定义变量
我的数据库中有 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;
最终输出:
第 7 行中的未定义变量 test3
第 7 行中的未定义变量 test4
第 7 行中的未定义变量 test5< /code>
Test1, Test2
我想出了 3 种可能的方法来希望用 NULL 值运行代码,看看是否会得到一个没有错误的空白页,不幸的是,它们都给了我“Underfined Variable”错误:
if(isset($result['test3'])){$test3= "确定"; echo $test3};
if(!empty($result['test3'])){$test3= "确定"; echo $test3};
if($result['test3']=='1'){$test3= "确定"; echo $test3};
帮忙吗?提前致谢!
I have 5 fields in my DB:
test1 = 1, test2 = 1, test3 = NULL, test4 = NULL, test5 = NULL
PHP code:
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;
Finaly Output:
Undefined Variable test3 in Line 7
Undefined Variable test4 in Line 7
Undefined Variable test5 in Line 7
Test1, Test2
I came up with 3 possible ways to hopefully run the code with NULL values to see if I will get a blank page with no error, unfortunately, they all gave me "Underfined Variable" error:
if(isset($result['test3'])){$test3= "OK"; echo $test3};
if(!empty($result['test3'])){$test3= "OK"; echo $test3};
if($result['test3']=='1'){$test3= "OK"; echo $test3};
Help? Thanks in Advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种无需
array_filter
即可实现的方法:A way to do it without
array_filter
: