Question: I have this php code:

```
$count = 8;

    echo '<div style="background-color:green">';

    for($i=1; $i<=$count; $i++) {
        echo $i;

        if($i%2 == 0) {
            echo '</div><div style="background-color:green">';
        }
    }

    echo '</div>';

```

What this will do is to wrap the numbers in a div with green background color every 2. I wanted to make something like in the snippet

```html
<div style="background-color:green">12</div>
<div style="background-color:yellow">34</div>
<div style="background-color:green">56</div>
<div style="background-color:yellow">78</div>
```

how can i do this in php? please help. Thanks in advance =)

Answer: 
You can do this using if else condition . suppose if $i is odd then color yellow then other green . i have added some option which give you same result.

**Option 1.** 

```
<?php
    $count = 8;
    for($i=1; $i<=$count; $i++) {
        $color = $i%2 == 0 ? 'green' : 'yellow';
        echo '<div style="background-color:'.$color.'">'.$i.'</div>';
    }

```

**Option 2.**

```
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
    if ($i%2 == 0) {
        $color = 'green';
    } else {
        $color = 'yellow';
    }
    echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}

```

**Option 3.**

```
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
    $color = 'yellow';
    if ($i%2 == 0) {
        $color = 'green';
    }
    echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}

```
maybe this? 

```
$count = 8;

        for($i=1; $i<=$count; $i++) {        

            if($i%2 == 0) {
                echo '</div><div style="background-color:green">'.$i;
            }else{
                echo '<div style="background-color:yellow">'.$i;
            }

            echo '</div>'
        }

```