1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Combining FOR loop with IF statement

Discussion in 'PHP' started by Grendor, Apr 19, 2005.

  1. #1
    For example if I want to color every even(every number that divided with 2 equals mod 0) number red in a sequence of 20. I think it should be something like this, but the coloring doesn't work:

    <html>
    <body>
    <?
    for ($a=1; $a<=20; $a++)
    if ($a % 2=="0") $color="red";
    {
    echo $a;
    echo "<br>";
    }
    echo "<font color=\"$color\">\n";

    ?>
    </body>
    </html>
     
    Grendor, Apr 19, 2005 IP
  2. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if ($a % 2=="0") $color="red";

    I think you want if ($a % 2== 0 ) $color="red";

    hth,

    tom
     
    TommyD, Apr 19, 2005 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    T0PS3O, Apr 19, 2005 IP
  4. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    for ( $i=0; $i<=20; $i++)
    {
       $color = ($i%2) ? "red" : "black";
    
       echo "<font color=\"$color\">$i is $color</font><br />\n";
    }
    
    PHP:
     
    rvarcher, Apr 19, 2005 IP
  5. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #5
    It's not a divisor, it's a modulus operator. :/
     
    noppid, Apr 19, 2005 IP
  6. mushroom

    mushroom Peon

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Your syntax is wrong
    for ($a=1; $a<=20; $a++)
    {if ($a % 2=="0")
    { $color="red";
    echo $a;
    echo "<br>";
    }
    else { optional statment;}
    }
    
    PHP:
    you are missing the extra " { } "s that makes it work properly, also a "{" must follow the ")"
     
    mushroom, Apr 19, 2005 IP
  7. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    for ($a=1; $a<=20; $a++)
    {
    	if ($a % 2=="0")
    	{ 
    		$color="red";
    		echo $a;
    		echo "<br>";
    	}
    	else 
    	{ 
    		optional statment;
    	}
    }
    
    PHP:
    I see no problems with the syntax. The problem is when he compares the 0 to "0", php may not type cast the var properly for the test. === may not help either since they are going to cast differently.

    The solution us use the 0 as the comparator. The code is laid out and the braces balance fine.
     
    noppid, Apr 19, 2005 IP