I’ve made an incredibly basic library, with a single function to check if the user is logged in now or not.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lib {
public function is_logged_in()
{
$CI =& get_instance();
if($CI->session->userdata('uid') === FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
}
/* End of file Lib.php */
If I call the function from a controller, the code is definitely run and when not logged in it makes it into the if statement. I can add an echo to test that the code is definitely executed, but never is anything returned. If I change the return to a number, nothing is returned. If I change the return to a string, then and only then is stuff actually returned. I’m calling the function like $this->lib->is_logged_in(), and have added the library to the autoload.php file.
The return is definitely executed as the function is exited. error reporting is set to E_ALL. Why in the heck won’t this work?
(also yes I do realize the function isn’t complete and secure yet.)
If you are trying to
echosomething after thereturnin your function, it will not work by design, I think this is probably universal to all languages. Anything after thereturnwill not be executed. It will be parsed, but not executed.var_dump($this->lib->is_logged_in())should give youbool(true)orbool(false), or a "Trying to get property of non-object" error iflibis not loaded correctly.Unless there is something you aren’t sharing with us, the function should work as expected.
If you’re still in doubt, assign the return value to a variable, then
var_dump()the variable before youreturnit. Should be the same result.EDIT: Sorry, I missed this in the long stream of comments:
I don’t believe that echo’ing
FALSEshould give you any output, but echoingTRUEshould give you1.This also has nothing to do specifically with Codeigniter.
For all practical purposes, there’s no good reason to
echothe return value of this function, or really anything where you expect a boolean return value. If it is returning the value you expect, then it is working.