0

I have a PHP function I'm using to determine if my user is logged in or not, it's very simple, and the code by itself works. I've never used functions much before, but I want to start. So I have my function is_logged(), which returns true if the user is logged in, obviously false if not. But when using the function, it is always returning true, no matter the circumstance. If I take the code within the function, and plainly have it outside of a function, it works, but if I put it back in the function, even if the user is logged out, it returns true.

is_logged Function

function is_logged(){
    if(!empty($_SESSION["user"])){
            return true;
    } else {
        return false;
    }
} 

How I'm trying to test the function.

if(is_logged) { echo "<br />Logged In"; } else { echo "<br />Not Logged In"; }

But the function only works if I take the inner code out of the function...I'm very confused as to why it's not working. The function is listed in a spot where the session variables can be read.

1
  • 3
    use is_logged() it is a function call Commented Jul 8, 2013 at 2:34

3 Answers 3

4

Change

if(is_logged)

TO

if(is_logged())

And your function can easily be simplified ...

function is_logged() {
    return !empty($_SESSION["user"]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps not one of my smartest moments...DOH! Well, I thank you, and that is going to go down in one of my funniest stupid moments while coding PHP. :P
It can be shortened even more to remove that unnecessary ternary conditional: return !empty($_SESSION["user"]);. Done!
2

You call is_logged() function without () (brackets). Also I suggest to optimize it with following:

function is_logged(){
    return !empty($_SESSION["user"]);
}

if(is_logged()){ echo "<br />Logged In"; } else { echo "<br />Not Logged In"; }

There is no need in if-statement, if you goint to return boolean result. It will only slow the function down.

4 Comments

It'll only slow the function down by a matter of microseconds which will go unnoticed
@DarylGill Microsecond here, microsecond there... Well, total there might be a second, if this function called very often :). Also, I do not see benefits of using if in if here. Are you?
I guess it's down to style and how the programmer him/herself decides to code :) if the OP is using functions for the first time, it should be focused on getting to grips with the usage, hence making it readable with commends and making the code self explanatory. Your example and babas' example may be the most optimized, Readability is the key for a noice
@DarylGill Yeap. I guess OP is just debuging with global if. Still, I hope, that OP may see benefits from here later.
1

Try making it more readable on your end:

if (is_logged) 
{
 echo "<br />Logged In"; 
}
else 
{ 
 echo "<br />Not Logged In"; 
}

Try making it like this:

if (is_logged() === true){
  // Function has returned true so act accordingly  

}else{
 // If here. Function has returned false, so act accordingly 
}

Why use: is_logged()?

Well, this indicated a function call.. Whereas using is_logged will make PHP look for a defined constant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.