2
echo "Point1, a=".$a."\n";
echo "Point1, b=".$b."\n";
if(1<2)
    {
        $a = 6; 
        $b['link'] = "here";
        echo "Point2, a=".$a."\n";
        echo "Point2, b[link]=".$b['link']."\n";
    }
echo "Point3, a=".$a."\n";
echo "Point3, b[link]=".$b['link']."\n";

Why does the above code print out the following?

Point1, a=
Point1, b=
Point2, a=6
Point2, b[link]=here
Point3, a=6
Point3, b[link]=here

In my understanding, the scope of $a and $b should end within the curly braces { }!

4 Answers 4

11

In my understanding, the scope of $a and $b should end within the curly braces { }!

Only functions and methods have their own, local scope. Other control structures (loops, conditions...) do not.

Variable scope in the PHP manual

Sign up to request clarification or add additional context in comments.

3 Comments

@tzmatt7447 To see why this is the desired behaviour, think of code like if (1<2) { a=true; b="yellow";} else { a=false; b="blue"; } This wouldn't be possible if scope worked like you're suggesting
@Michael - I see now how php does it, but what you're saying should be valid if a and b are declared beforehand... am i thinking too much C/C++?? :)
"am i thinking too much C/C++?" - Yes, probably. Php is fine with stuff like $ar[] = 7, which declares an array, creates it and adds its first element all in one go.
1

The first $a and $b would actually throw a warning, undefined index as they haven't been declared before being output.

3 Comments

True and good point, but should be a comment as it has nothing to do with the question
Well I thought that breaking the whole code was to do with the question.
No, variable scope was to do with the question. Pekka is correct. This is a very good and valid point, not an answer.
1

In PHP only functions introduce a new scope, unlike Java and other languages :((( So If you need a new scope you need to create a new function and call it.

$a = 1;


 call_user_func(function(){
     //new local scope
     $a = 99999;
});


echo $a;   //will print 1

Comments

0

Scope is the boundary of where you can access a variable (or property or method). Your code isn't an example of scope, it's syntax parsing. Within double quotes, php will recognize and try to evaluate variables. Because $b[..] is how you refer to an array element, php will try to parse it as such.

Curly braces are used for multiple things. In the context of your code, they delimit the beginning and end if your if(...) condition, as in

if (condition)
  {
    // do all
    // of this stuff
    // between the { and }
    // if the condition
    // is true
  }

This has nothing to do with scope, unless you wanna look at it in the sense of "this is where the code to be executed if the condition is true starts and ends" but as mentioned, that's not what "scope" really means.

You can also use {..} to tell php where to start and end the variable name, to avoid ambiguity. For example:

In this example, php will try to parse the variable as $abar because that is a valid variable name:

$a = "foo";
$b = "$abar"
echo $b; // output : nothing - $abar doesn't exist (will give you a notice)

Since you want it to parse $a not $abar, you would use {..} to specify the beginning and end of the variable name:

$a = "foo";
$b = "${a}bar"
echo $b; // output : foobar

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.