tags:

views:

124

answers:

4

I am just studying other user's PHP code right now to understand and learn better. In the code below, it is part of a user class. When I code using if/else blocks I format them like this...

if(!$this->isLoggedIn()){
    //do stuff
}

But in the code below it is more like this

if (! $this->isLoggedIn())
    return false;

Also in the function below you can see that there is a couple times that there can be a RETURN value. SO my question here, when RETURN is called, does it not run any code after that? Like does it end the script for that function there?

In this case if this is ran...

if (! $this->isLoggedIn())
        return false;

Does it continue to run the code below that?


Here is the function

<?PHP
private function logout($redir=true)
{
    if (! $this->isLoggedIn())
        return false;

    $this->obj->session->sess_destroy();

    if ($this->isCookieLoggedIn())
    {
        setcookie('user','', time()-36000, '/');
        setcookie('pass','', time()-36000, '/');
    }
    if (! $redir)
        return;

    header('location: '.$this->homePageUrl);
    die;
}
?>
+7  A: 

Yes.

When PHP sees a return command, it stops executing and returns it to whatever called it. This includes includes, function executions, method execution, etc.

In the following, 'Test' will never echo:

$test = "test";
return;
echo $test;

If you are in an included file, return will stop its execution, and the file that included it will finish executing.

One of the use cases is similar to what you described:

public function echoString($string)
{
    if(!is_string($string))
    {
        return;
    }
    echo $string;
}
Chacha102
+1 plus best answer again, awsome thanks so much, I never knew this!
jasondavis
"If you are in an included file, return will stop the execution of the file, and the file that included it will finish executing.". I'm pretty sure that's incorrect -- a return in an included file does NOT halt the execution of the file that does the including. Specifically, it's possible to assign the value of a return statement in an included file to a variable. e.g. if file1.php says `return '5';` and file2.php says `$x = include('file1.php'); echo $x;`, file2.php should echo the string '5'.
Frank Farmer
@Frank, you read it wrong. I said it halted **its** execution and the file that included it will finish executing. Which is exactly what you quoted. And yes, according to what I said in my answer, the code you just provided will echo '5'
Chacha102
Ah, I misunderstood what you meant by "the file that included it will finish executing". I thought you were using "finish executing" as a synonym for "halt" -- it's a little ambiguous. Might clarify that to "the file that included it will *continue* executing".
Frank Farmer
+1  A: 

Start by reading the manual:

http://us2.php.net/return

AJ
good idea, thanks
jasondavis
A: 

I think we need to distinguish between return used within a function and return used globally.

As the PHP Function Reference says, script execution is only stopped in the second case.

@jason, you seemed to be asking about its use in a function.

pavium
Hello, yes in this case it is in a function or class method, you are saying it is only stopped in the second case being used glabally?
jasondavis
Technically, return in both cases stops the execution and gives it up to whatever started the execution, whether it be an include, a function/method call, or PHP itself. A return in the highest level file will terminate the script.
Chacha102
Yes, the manual says "If called from the global scope, then execution of the current script file is ended." I didn't think this is what you meant.
pavium
It stops the execution of the current scope, returning back to the line that called it. When used in the global scope, there simply isn't anywhere in the PHP code to return to, so it ends the execution of the code. - In a sense it returns the thread back to the command that executed the PHP script.
Atli
+1  A: 

As a side-note...

Even though the return keyword can be used like this, many would consider using it in the manner it is used in your example function to be a very bad practice. It can mess with the "flow" of the code, making it less readable. (Similar to using the goto statement, though admittedly not as bad.)

I would argue that the code you posted would be better structured like this:

<?php
function logout($redir=true)
{
    if ($this->isLoggedIn()) 
    {
        $this->obj->session->sess_destroy();

        if ($this->isCookieLoggedIn()) {
            setcookie('user','', time()-36000, '/');
            setcookie('pass','', time()-36000, '/');
        }

        if ($redir) {
            header('location: '.$this->homePageUrl);
            die;
        }
    }
}
?>

Nowhere in this version does the code "break" out of a block early. There is never any doubt as to whether the following lines should be executed or not.

Atli
I'd argue that there should be a line between the two if statements and you should use consistent placement of brackets.
Chacha102
This is how I am used to coding, thats why the code I posted confused me a lil bit, thanks for clarifying that this is a good way
jasondavis
@Chacha102. Agreed. I've updated it to look how I would normally format it *(What I posted originally was more of an edit of Jason's original code than a complete rewrite)*. - Not sure how you define *consistent bracket placement*, but my rule-of-thumb is to put the opening bracket in a new line if the block it opens contains other blocks.
Atli
Seems we edited it at roughly the same time, ending up fixing one bracket and breaking the other one xD - I fixed it; made it look like how I would have written it. - Would you consider putting *all* brackets on new lines better? *(This looks cleaner to me, somehow.)*
Atli
I think it does actually. Making a space between the if statement and the first line of code spreads it out more. And the bracket basically does that.
Chacha102
I guess it comes down to personal taste. - I started out using Java, where brackets are typically not put on a new line, but then moved on to C# where they are *(forcibly, by Visual Studio)*. It seems I've combined the two styles when working on PHP :) - Also, JavaScript is often written as C# is, but indenting each bracket one tab further than C# does. That's a horrific style though, in my opinion. (Like: http://www.w3schools.com/Ajax/ajax_example_suggest.asp)
Atli