Foreach, If and Variables - Weird variable evaluation

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

    Foreach, If and Variables - Weird variable evaluation

    Hi,

    I am having a rather frustrating time with a script I am working on.

    I am looping through the elements of an array with a foreach statment
    and inside the foreach statement I have two if statements.

    An an example:

    foreach ($a as $b) {
    if ($b == $value1) {
    statement1;
    }
    if ($b == $value2) {
    statement2;
    }
    }

    If I replace $value1 and $value2 with actual values, both if statements
    are tested. However is I leave things as they are using varibles
    containing the values I want to check for only the first if statement
    is tested. It is acting as though it is breaking out of the foreach
    when the first if statement evaluates to true. This does not happen
    with actual values in place of $value1 and $value2 variables.

    If anyone can shed light on this problem, the help is greatly
    appreciated.

    TIA,
    Adam

  • Alvaro G Vicario

    #2
    Re: Foreach, If and Variables - Weird variable evaluation

    *** Adam wrote/escribió (27 Jun 2005 21:13:04 -0700):[color=blue]
    > foreach ($a as $b) {
    > if ($b == $value1) {
    > statement1;
    > }
    > if ($b == $value2) {
    > statement2;
    > }
    > }
    >
    > If I replace $value1 and $value2 with actual values, both if statements
    > are tested. However is I leave things as they are using varibles
    > containing the values I want to check for only the first if statement
    > is tested. It is acting as though it is breaking out of the foreach
    > when the first if statement evaluates to true. This does not happen
    > with actual values in place of $value1 and $value2 variables.[/color]

    Your pseudo-code looks correct, so the problem must in the actual code you
    don't paste.


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    Working...