PHP奇怪的NULL变量情况

发布于 2024-09-24 23:37:45 字数 1639 浏览 1 评论 0原文

我在这个方面遇到了困难,我有一个填充数组的类。

有时数组不会被填充,我想在发生这种情况时做一个边缘情况,但我似乎无法判断它何时为空!代码如下:

$results_dev = $class->get_data_nologin("4a9d-4f41-9566-7705",$key,"Sensors");
    echo "something";
    print_r($results_dev);

    if(is_null($results_dev))
    {   
        echo "thisisNULL";
        $counter--;
    }

“something”确实得到了回显,但是当我 print_r $results_dev 时,它打印“null”并且 if 语句永远不会执行!我也尝试过

if(!$results_dev)

and

if($results_dev == "null")

and

if($results_dev == null)

但它仍然不执行 if 循环中的内容,我还能如何检查这个?

顺便说一句:我的类卷曲 API 并检索 JSON,但是有时卷曲中的 URL 不正确,因此它返回 null。更糟糕的是在这之后我有:

if($results_dev['location'])
    {
        echo "data".$results_dev['location'];

即使它为空它也会执行!它打印“数据”,然后就什么也不打印了。

get_data_nologin 的代码:

    $url = 'https://api.url/'.$scf.'/'.$deviceid.'?key='.$apikey;
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($curl_handle, CURLOPT_COOKIEFILE, $cookie_file_path);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    if (empty($buffer)) 
    {
        echo 'Something went wrong :(';
    } 
    else 
    {

            $decodedBuffer = json_decode($buffer, TRUE);

            return $decodedBuffer;

    }

I'm having a tough time with this one, I have a class which populates an array.

Sometimes the array will not be populated and I want make an edge-case for when this happens however I can't seem to tell when its null! Here's the code:

$results_dev = $class->get_data_nologin("4a9d-4f41-9566-7705",$key,"Sensors");
    echo "something";
    print_r($results_dev);

    if(is_null($results_dev))
    {   
        echo "thisisNULL";
        $counter--;
    }

"something" does get echoed out, but when I print_r $results_dev, it prints "null" and the if statement never executes! I've also tried

if(!$results_dev)

and

if($results_dev == "null")

and

if($results_dev == null)

But it still doesn't execute whats in the if loop, how else could I check this?

BTW: my class curls an API and retrieves a JSON, however sometimes the URL is incorrect in curl so it returns null. What's even worse is after this I have:

if($results_dev['location'])
    {
        echo "data".$results_dev['location'];

And It executes even when its null! It prints "data" and absolutely nothing afterwards.

code for get_data_nologin:

    $url = 'https://api.url/'.$scf.'/'.$deviceid.'?key='.$apikey;
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($curl_handle, CURLOPT_COOKIEFILE, $cookie_file_path);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    if (empty($buffer)) 
    {
        echo 'Something went wrong :(';
    } 
    else 
    {

            $decodedBuffer = json_decode($buffer, TRUE);

            return $decodedBuffer;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-10-01 23:37:45

使用 isset($results_dev)

if(!isset($results_dev)){
 echo "thisisNULL";
        $counter--;

}

有了新的输入,这可以是解决方案:

if(strtolower($results_dev) == 'null'){ 
     echo "thisisNULL";
            $counter--;

}

use isset($results_dev)

if(!isset($results_dev)){
 echo "thisisNULL";
        $counter--;

}

With new inputs this can be the solution:

if(strtolower($results_dev) == 'null'){ 
     echo "thisisNULL";
            $counter--;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文