I have written a function that returns data on success and false on failure.

I was wondering if this is considered a proper way of evaluating the return value in php.

function data(){
  if($all_is_good){
    return $data
  }else{
    return false;
  }
}

$data = data(); //basically either a long series of strings or arrays

if(!$data){  //<-- this is the line I'm concerned about, is it safe to assume this?
   echo 'oh no, bad stuff happened';
}

Thank you so much!

Accepted Answer

What you have there is fine, although alternatively, you could use if($data === false) {. This ensures that $data is actually false, considering 0 and NULL are also seen as false.

Written by TVK-
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki