1

Someone please explain to me why this doesn't work, and what I am doing wrong. For some reason, when I run the function validateUsername, the $error variable remains completely unchanged, instead of evaluating to true. How is this possible?

Yet, if I remove the code within the function and run it straight without a function call, it works. The example below is so simple it is practically pseudo code, and yet it doesn't work. Is this behavior unique to PHP? I don't want to run into this again in some other language.

<?php

$username = 'danielcarvalho';
$error = false;

function validateUsername()
{
    if (strlen($username) > 10)
    {
        $error = true;
    }
}

validateUsername();

if ($error == false)
{
    echo 'Success.';
}
else
{
    echo 'Failure.';
}

?>
3
  • 3
    Read up on variable scope: php.net/manual/en/language.variables.scope.php Commented Apr 16, 2011 at 13:37
  • 1
    In addition to variable scope, also read up on returning values from functions Commented Apr 16, 2011 at 13:40
  • 1
    It's just in a function on the same page, surely this can't be a variable scope issue? Guess I'm wrong. I'll read up. Commented Apr 16, 2011 at 13:42

2 Answers 2

7

This isn't working because $username isn't available within the scope of your validateUsername function. (Neither is the $error variable.) See the variable scope section of the PHP manual for more information.

You could fix this by adding global $username, $error; within your function, although this isn't a particularly elegant approach, as global variables are shunned for reasons too detailed to go into here. As such, it would be better to accept $username as an argument to your function as follows:

<?php
    function validateUsername($username) {
        if (strlen($username) > 10) {
            return false;
        }

        return true;
    }

    if (validateUsername('danielcarvalho')) {
        echo 'Success.';
    }
    else {
        echo 'Failure.';
    } 
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks chap, great answer. I'm still just struggling to cope with the fact that it is a scoping issue. Seems illogical / impractical to me.
@Daniel It might seem that way initially - but just think about it as trying to keep the "locality" of variables to an absolute minimum. (In other words, as a means of obtaining the highest level of separation/isolation, which is always a good thing as you'll discover when you encounter things like object-oriented programming.) Happy travels! :-)
To be fair though, I'm very experienced in ActionScript and have done a moderate amount of C#, and both of them never had this strict scoping issue where they can't see a variable declared just outside of a function.
1

$error has local scope in function validateUsername. To access global variables, use global keyword.

Read about scopes here. Change your function to:

function validateUsername($username)
{
    global $error;
    if (strlen($username) > 10)
    {
        $error = true;
    }
}

validateUsername($username);

Better implementation using function parameter:

function validateUsername($username, &$error)
{
    if (strlen($username) > 10)
    {
        $error = true;
    }
}
validateUsername($username, $error);

Another implementation:

function validateUsername($username)
{
    if (strlen($username) > 10)
    {
        return true;
    }
    return false;
}
$error = validateUsername($username);

6 Comments

global variables aren't the answer to all the world's problems: they're likely to cause more problems than they solve, and aren't to be recommended when function arguments and return values are a more correct approach
@Ashwini Both of your solutions are a bit awry. In the first one, you need to use &$error in the function argument (otherwise, $error won't be updated in the "calling" scope) and I'd really avoid the needless use of a global as far as the second one is concerned.
Your second example will not work as expected - variables are passed by value by default, your global $error will not change whatever you do inside the function.
I voted you up for the detailed answer as well. Feels wrong not to award you with something. Hmmm, interesting comments though, will keep these concerns in mind.
@middaparka and Maelyn. Don't know how did I write it in first place! Have edited it.
|

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.