0

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;

    }
5
  • Can you show us the code for $class->get_data_nologin? Commented Sep 27, 2010 at 17:41
  • 1
    If the result of the function call is actually the NULL value, your code should be working -- see ideone.com/hBYk5. Commented Sep 27, 2010 at 17:44
  • Could the json_decode perhaps be not sending a true NULL value? Commented Sep 27, 2010 at 17:46
  • According to its manual page: Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. Commented Sep 27, 2010 at 17:47
  • 1
    As Daniel says, it should work. Most likely there's just an error in the name of the variable (do you show E_NOTICE errors on your dev-server?), or you get a really unexpected result in json (the string 'null' for instance, possibly surrounded with xml/html tags you cannot see, but would explain why $results_dev['location'] works but seemingly displays no data). Commented Sep 27, 2010 at 17:48

1 Answer 1

3

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

}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for responding, isset($results_dev) isn't triggering anything either
If $class->get_data_nologin in OP's code returns an actual NULL value, is_null should work just as well as isset...
@Daniel : Correct !! @Pete: stupid question, but.. does it return null as a string ? try this gettype($results_dev) and see what you get
OK .. then do this: if(strtolower(gettype($results_dev)) == 'null'){
Its not triggering anything, wouldn't that look like if(string == 'null')?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.