Jump to content

[SOLVED] why does empty() not work on object ?


anatak

Recommended Posts

hello

 

$error= $db->ErrorMsg();

if(empty($error)){echo 'empty';}

 

works fine

 

if(empty($db->ErrorMsg())){echo 'empty';}

 

does not work.

Can someone explain why ?

 

related to empty() I also have this non OOP question

if(!isset($var)){echo 'var is not set';}

works but

if(!empty($var)){echo 'var is not set';}

does not work

 

Is there a reason why you can not test for !empty() ??

 

kind regards

anatak

 

thank you

 

Link to comment
Share on other sites

On that note, why not create a function in the class that checks the errormsg variable for you something like:

 

<?php

if (!$db->errorEmpty()) {

}



// (note this should be in the class with the right values
function errorEmpty() {
   if (!empty($this->error)) {
          return true;
   }

   return false;
}
?>

 

Should work just the same as you intended it, just a little bit less code and less chance for error as you do not have to add !empty() to ever if statement like this it is simply if !$db->errorEmpty()  =)

Link to comment
Share on other sites

sorry for the long wait.

 

About the empty() function.

 

I just wondered why !empty($var) does not work.

 

if(empty($var)){echo $var .'is empty';} //this works

if(!empty($var)){echo $var .'is not empty';} //this does not works

 

anyway I can work around it so it is not much of a problem.

Thanks for the help with understanding $db->ErrorMsg();

 

 

Link to comment
Share on other sites

  • 5 months later...

Jenk,

 

I think that empty($var) checks to see if $var exist (not defined), if $var=null or if $var="" (empty string)

if the $var is in any of these conditions the empty() function will return true

I was just wondering why !empty() in an if statement did not seem to work.

 

anyway my problem is solved so I will close this topic

anatak

Link to comment
Share on other sites

You still didn't read the first reply properly.

 

empty($var) will return true if either $var is undefined, or contains the value of null, 0, "" (empty string) or an empty array.

 

I suggest you read that line very carefully, then read it again carefully, and once more for luck, before replying again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.