![]() |
Checking if a string is empty? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Checking if a string is empty? (/showthread.php?tid=61529) |
Checking if a string is empty? - lexxtoronto - 04-22-2015 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')=="") 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')) RE: Checking if a string is empty? - Narf - 04-22-2015 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. RE: Checking if a string is empty? - Rufnex - 04-22-2015 Check for NULL, as Narf said. http://php.net/manual/de/function.is-null.php RE: Checking if a string is empty? - lexxtoronto - 04-22-2015 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. RE: Checking if a string is empty? - ivantcholakov - 04-22-2015 @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()) { Thus, when the time comes for a upgrade you will have limited number of places to check for compatibility (FALSE -> NULL on nothing). RE: Checking if a string is empty? - lexxtoronto - 04-22-2015 ivantcholakov - that's a nice advice, thank you! RE: Checking if a string is empty? - cartalot - 04-22-2015 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. RE: Checking if a string is empty? - lexxtoronto - 04-22-2015 @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... RE: Checking if a string is empty? - cartalot - 04-23-2015 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 ; RE: Checking if a string is empty? - lexxtoronto - 04-23-2015 @cartalot, Ah ok I understand, that's interesting too, thank you! |