Re: Variable Scope

From: Date: Sun, 31 Aug 2003 10:16:08 +0000
Subject: Re: Variable Scope
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-4310@lists.php.net to get a copy of this message
Perhaps this is an example from C++, but it illustrates my point. This may be a better example, if we are to step back a decade or two: int i; for (i = 0; i < 5; i++)
What is so 'inherently declaratory' about this for() ? With this example, you've illustrated the exact reason why this is not possible in PHP. The first part of a for() is for declaration _and_ initialization. Without mandatory declaration (like in PHP), the compiler will not be able to distinguish the two cases. In terms of your own example:
$array = array(1, 2, 3, 4, 5); for ($i = 0; $i < 5; $i++) {
    $num = $array[$i];
    echo $num;
    for ($i = 0; $i < 5; $i++)
    {
        echo $num * $i;
    }
}
'The whole thing gets screwed up' is only defined in terms of what you expect the result to be. How is the compiler to know that you're asking for a new variable in the inner for() loop ? It doesn't crash, does it ? As the number of possible variable names in PHP is sufficient for most applications, using the same variable name for different variables is just /bad/ coding style. Ard

« previous php.internals (#4310) next »