Welcome Guest, Not a member yet? Register   Sign In
Checking if a string is empty?
#1

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:

Code:
if($this->session->userdata('username')=="")
{
   //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'))
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#2

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

Check for NULL, as Narf said.

http://php.net/manual/de/function.is-null.php

Reply
#4

(This post was last modified: 04-22-2015, 10:06 AM by lexxtoronto.)

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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#5

@lexxtoronto

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:

Code:
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).
Reply
#6

(This post was last modified: 04-22-2015, 10:57 AM by lexxtoronto.)

ivantcholakov - that's a nice advice, thank you!
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#7

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.
Reply
#8

@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...
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#9

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

@cartalot, Ah ok I understand, that's interesting too, thank you!
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB