PHP奇怪的NULL变量情况
我在这个方面遇到了困难,我有一个填充数组的类。
有时数组不会被填充,我想在发生这种情况时做一个边缘情况,但我似乎无法判断它何时为空!代码如下:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 isset($results_dev)
有了新的输入,这可以是解决方案:
use isset($results_dev)
With new inputs this can be the solution: