开发者

Variable Undefined in PHP

开发者 https://www.devze.com 2023-03-16 21:14 出处:网络
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};

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开发者_如何学C 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:

  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};

Help? Thanks in Advance!


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


A way to do it without array_filter:

 $result = array();

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

 $total = implode(", ", $result);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号