Variable scope issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rlee0001

    #1

    Variable scope issue

    This is seriously blowing my mind.

    $a = array('a', 'b', 'c');
    for ($i=0; $i<=count($a); $i++) {
    echo 'In FOR: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
    if ($a[$i] = 'a') {
    echo 'In IF: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
    }
    }

    Ok, I expected the above to output:

    In For: a - b - c
    In IF: a - b - c
    In For: a - b - c
    In For: a - b - c

    But I get this:

    In For: a - b - c
    In IF: a - -
    In For: a - b - c
    In For: a - b - c

    For some reason $a[1] & $a[2] can be seen outside the IF but inside the
    IF always returns NULL except when the element in the IF is used as a
    condition (like $a[0]). I've been coding in PHP for a while now and I
    have never noticed IF blocks having their own scope. But I am using an
    older version of PHP (4.3.3) on this server (not my choice) which I've
    never used before. If IF statements do have their own scope GLOBAL
    doesn't fix it because I tried that and got the same results.

    I think I'm loosing my mind. Please help.

    -Robert

  • Oli Filth

    #2
    Re: Variable scope issue

    rlee0001 said the following on 01/06/2006 18:31:[color=blue]
    > This is seriously blowing my mind.
    >
    > $a = array('a', 'b', 'c');
    > for ($i=0; $i<=count($a); $i++) {
    > echo 'In FOR: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
    > if ($a[$i] = 'a') {
    > echo 'In IF: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
    > }
    > }
    >[/color]

    "==" vs. "="...


    --
    Oli

    Comment

    • rlee0001

      #3
      Re: Variable scope issue

      > "==" vs. "="...

      Thanks for the quick response Oli. I actually made that error in
      creating the example I posted. My orignial code actually does a "!=". I
      found that assigning to variables within the FOR works...eg:

      $a = $myarray['a'];
      $b = $myarray['b'];
      $c = $myarray['c'];

      Then I can use $a..$c within all structures inside the loop fine. Very
      strange.

      -Robert

      Comment

      • Chung Leong

        #4
        Re: Variable scope issue


        Oli Filth wrote:[color=blue]
        > rlee0001 said the following on 01/06/2006 18:31:[color=green]
        > > This is seriously blowing my mind.
        > >
        > > $a = array('a', 'b', 'c');
        > > for ($i=0; $i<=count($a); $i++) {
        > > echo 'In FOR: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
        > > if ($a[$i] = 'a') {
        > > echo 'In IF: '.$a[0].' - '.$a[1].' - '.$a[2]."\n";
        > > }
        > > }
        > >[/color]
        >
        > "==" vs. "="...
        >
        >
        > --
        > Oli[/color]

        That doesn't explain the problem he/she described. As written, the code
        would loop forever, since a new row is added at the end whenever $i ==
        count($a).

        Obviously we can't diagnose code with typos...

        Comment

        • Bent Stigsen

          #5
          Re: Variable scope issue

          rlee0001 wrote:[color=blue][color=green]
          >> "==" vs. "="...[/color]
          >
          > Thanks for the quick response Oli. I actually made that error in
          > creating the example I posted. My orignial code actually does a "!=". I
          > found that assigning to variables within the FOR works...eg:
          >
          > $a = $myarray['a'];
          > $b = $myarray['b'];
          > $c = $myarray['c'];
          >
          > Then I can use $a..$c within all structures inside the loop fine. Very
          > strange.[/color]

          It really shouldn't be necessary. Give your code that produced the
          erroneous output another look, or post it here.

          /Bent

          Comment

          Working...