CodeIgniter Forums

Full Version: Checking if a string is empty?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've seen a number of times where people check if the string is empty even if there is no empty string. For example checking if a username string is present in a session cookie.

Ive seen people do this:

   //user not logged in
else { //user logged in }

Which will work because PHP is a loosely typed language. But the problem is that there is no empty string (unless you specifically insert an empty string), if you havent inserted anything the value will be NULL. NULL and empty string are different entities.

So if you check against the type too e.g. if($this->session->userdata('username')==="") then the code will fail.

The better and shorter way is to do something like this - if($this->session->userdata('username')) - Because it returns TRUE if it exists and FALSE otherwise. Or if you want to reverse the logic then - if(!$this->session->userdata('username'))
The topic title suggests that this is a question, yet the content looks like a recommendation ... and a wrong one.

$this->session->userdata('username') returns the value of your 'username' item (or NULL if it doesn't exist, in CI3), not TRUE/FALSE.
Check for NULL, as Narf said.
Yes, I have to mention the version too, I guess, I was referring to CI2. CI 2 session article says - Note: The function returns FALSE (boolean) if the item you are trying to access does not exist. Yes, it returns the value of 'username' if it exists and if any variable exists then it will be true. I keep forgetting about CI3, not ready to switch yet. Yes, it returns NULL in CI3 the way it should've been from the very beginning I guess.

I would recommend you not to work everywhere with the session directly, hide these implementation details within a class and then use it this way:

if ($this->auth->is_logged_in()) {
    // Do this.
} else {
    // Do that.

Thus, when the time comes for a upgrade you will have limited number of places to check for compatibility (FALSE -> NULL on nothing).
ivantcholakov - that's a nice advice, thank you!
another way to do this that is very flexible - validate the session and then return something useful like a $user object,
and if it doesn't come back then show the log in.

PHP Code:
if( ! $user $this->users->returnActiveUser() ){ $this->showLogin() ; } 

that way you can change the session validation in users but the controller that is calling it does not have to change.
@cartalot - Thank you, you mean validate the username? Im using ci_sessions table so the session is being validated automatically. Your suggestion sounds interesting, but Im not sure if I understand...
so following from how ivantcholakov was saying to hide the implementation details in a class...
with that example its going to return a true / false whether the user is logged in -- and sometimes thats all you need. but you can take the same idea one step further and instead of just returning "yes the user is logged in" - you use your session identifier to return an object (or array etc) that you can use in your class / model / view.

PHP Code:
echo 'hello '  $user->firstname '  '  $user->lastname 
@cartalot, Ah ok I understand, that's interesting too, thank you!