CodeIgniter Forums
What is better when return false - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: What is better when return false (/thread-68365.html)



What is better when return false - wolfgang1983 - 06-29-2017

Hello when using return false I would also like to know if return "" is OK 

Or which one is better over the other.

PHP Code:
public function getattachments($where_column ''$id '')
{
    if (
$id) {
     
   $this->db->where($where_column$id);
    } 

    
$query $this->db->get($this->db->dbprefix $this->table_name);

    if (
$query->num_rows() > 0) {
     
  return $query->result_array();
    }

    return 
"";
    




RE: What is better when return false - Paradinight - 06-29-2017

(06-29-2017, 07:42 PM)wolfgang1983 Wrote: Hello when using return false I would also like to know if return "" is OK 

Or which one is better over the other.

PHP Code:
public function getattachments($where_column ''$id '')
{
 if (
$id) {
 
   $this->db->where($where_column$id);
 } 

 
$query $this->db->get($this->db->dbprefix $this->table_name);

 if (
$query->num_rows() > 0) {
 
  return $query->result_array();
 }

 return 
"";
 


Return an empty array.


RE: What is better when return false - wolfgang1983 - 06-29-2017

(06-29-2017, 08:15 PM)Paradinight Wrote:
(06-29-2017, 07:42 PM)wolfgang1983 Wrote: Hello when using return false I would also like to know if return "" is OK 

Or which one is better over the other.

PHP Code:
public function getattachments($where_column ''$id '')
{
 if (
$id) {
 
   $this->db->where($where_column$id);
 } 

 
$query $this->db->get($this->db->dbprefix $this->table_name);

 if (
$query->num_rows() > 0) {
 
  return $query->result_array();
 }

 return 
"";
 


Return an empty array.

Something like return array();


RE: What is better when return false - Paradinight - 06-29-2017

(06-29-2017, 09:17 PM)wolfgang1983 Wrote:
(06-29-2017, 08:15 PM)Paradinight Wrote:
(06-29-2017, 07:42 PM)wolfgang1983 Wrote: Hello when using return false I would also like to know if return "" is OK 

Or which one is better over the other.

PHP Code:
public function getattachments($where_column ''$id '')
{
 if (
$id) {
 
   $this->db->where($where_column$id);
 } 

 
$query $this->db->get($this->db->dbprefix $this->table_name);

 if (
$query->num_rows() > 0) {
 
  return $query->result_array();
 }

 return 
"";
 


Return an empty array.

Something like return array();

yes or

PHP Code:
return $query->result_array(); 

without $query->num_rows() Smile


RE: What is better when return false - rtenny - 06-30-2017

Yes that's what I do as well

if the success case would return an array then i would return an empty array
if success would return a string then an empty return string

That way the return value is consistent


RE: What is better when return false - SomeGuy - 04-06-2018

Hope you decided to return NULL.

Returning an empty array when there are no results is not the same as returning an array with known keys. You'll have to validate the array before you can actually use it.

PHP Code:
if(NULL !== ($attachments $model->getAttachments())) : 
# Hey, I have valid attachments and not just some returned value that looks like I do.
endif; 



RE: What is better when return false - InsiteFX - 04-06-2018

PHP Code:
if (!empty($array))
{
 
   /**
     * empty() is very useful for dealing with PHP arrays since it returns true
     * if your array variable is null or an empty array, and also gracefully
     * returns true if your array variable is undefined.
     *
     * this code runs if $array is defined, not null, and not an empty array()
     */
 
   return $array;
}
else
{
 
   // $array is empty, null or undefined so return true
 
   return true;


empty is great for arrays, but very bad for using on strings "0".