Welcome Guest, Not a member yet? Register   Sign In
What is better when return false
#1

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

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(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.
Reply
#3

(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();
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

(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
Reply
#5

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
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#6

(This post was last modified: 04-06-2018, 12:30 AM by SomeGuy.)

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

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".
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB