0

I realize this is extremely simple but I feel I'm over-looking something. What I want to screen to display is


RED This is 0.

or

GREEN This is 1.

And for it to alternate back and forth between the displayed text. My logic if fine for alternating between Red and Green, but the "This is 0" and "This is 1" text is not displaying.

Here is my code so far:

<?php

$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;


while($count<10)
// DO 9 TIMES
{

    echo $array[$a_count] . ' ';
    //SUDO FOR IMAGE BEING DISPLAYED

    while($array[$a_count] == 0)
    {
        echo "This is 0.<br>";
    }

    while($array[$a_count] == 1)
    {
        echo "This is 1<br>";
    }


//<----SWITCH BACK AND FORTH---->
    if($a_count == 1)
    {
        $a_count = 0;
    }
    else
    {
        $a_count++;
    }
//<----------------------------->
    $count++;
}

?>

I realize the easiest way to get what I would like is:

<?php

$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;


while($count<10)
// DO 9 TIMES
{

    echo $array[$a_count] . ' ';
    //SUDO FOR IMAGE BEING DISPLAYED


//<----SWITCH BACK AND FORTH---->
    if($a_count == 1)
    {
        echo "This is 1<br>";
        $a_count = 0;
    }
    else
    {
        echo "This is 0.<br>";
        $a_count++;
    }
//<----------------------------->
    $count++;
}

?>

But this code does not contain the logic I need for the continuation of this project. I would greatly appreciate an answer as to why my first code is not printing "This is 0."

Thank you!

1
  • What logic r u looking for? You have answered your own question Commented Jul 31, 2013 at 16:47

6 Answers 6

3

What you are looking for is a mod count.

Change your loop for:

for($i=0;$i<10;$i++){
    echo $array[$i%2].' This is '.($i%2);
}

The modulo operator returns the remainder of a division. See: What are the practical uses of modulus (%) in programming?

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

Comments

1

Why not something like this:

$colors = array(0 => 'Red', 1 => 'Green');
$idx = 0;
$count = 0;
while($count < 10) {
    echo "The color is {$colors['$idx']}<br />";
    $count = 1 - $count; // if $count is 1, it becomes 0. if it's 0, it becomes 1
}

Your while() loops are basically totally useless. You're trying to compare the RED and GREEN strings again 0. If either evaluation happens to be true, you'll end up with an infinite loop.

Comments

0

Ignoring the inefficiency of this script, your while loops are comparing against the array's values, not its keys. But fixing this problem will actually expose another - an infinite loop.

Comments

0

You aren't changing the value of $a_count in your while loops, so there is no way for them to end.

The problem is here:

while($array[$a_count] == 0)
{
    echo "This is 0.<br>";
}

while($array[$a_count] == 1)
{
    echo "This is 1<br>";
}

Once it enters the first loop, it will just keep echoing "This is 0.<br>", as $a_count is unchanging.

It looks like you could change these whiles to ifs to make your code work the way you want. You also probably want to check that $a_count is 0 or 1, rather than $array[$a_count]

Comments

0

Well, in your first example before the while($count<10), have you initilize your values? If you do, you must have a display like that :

RED This is 0.0
This is 0.0
This is 0.0
This is 0.0
This is 0.0
...

"This is 0.0" is display in a infinite loop.

    while($array[$a_count] == 0)
    {
        echo "This is 0.$count<br>";
    }

    while($array[$a_count] == 1)
    {
        echo "This is 1<br>";
    }

You must change the value in a while loop.

Other tips, I think you mush take a look at "foreach" php loop. Can be useful for what you want to do. modulos can also help you.

3 Comments

Unfortunately, that is not what I'm getting This is what I'm getting is: RED GREEN GREEN GREEN GREEN GREEN GREEN GREEN GREEN
I also initialized $count while declaring my variables at the very beginning.
Do you have? : $array = array(0=>"RED",1=>"GREEN"); $a_count = 0; $count = 0; I copy/paste your first code, adding this at the begining.
0

I think probably the easiest way to do this is to just use a switch statement. I feel really silly for not thinking of it before.

while($count<10)
{

    echo $array[$a_count] . ' ';
    //PSUEDO FOR IMAGE BEING DISPLAYED

    switch($a_count):
    {
        case 1:
            echo "This is RED.<br>";
            $a_count = 0;
            break;

        case 0:
            echo "This is GREEN.<br>";
            $a_count++;
            break;

    }

    $count++;
}

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.