random case

Hi, I’m using code looking like this:

[php]

<? $num = rand(0,2); switch ($num) { case 0: echo "first sentence"; break; case 1: echo "second sentence..."; break; case 2: echo "third sentence..."; break; } ?>

[/php]

Problem is that every third time (or so) nothing at all is being echoed. Anyone have any idea why?
The code above is only a shorter passage of the original code.

//ttb.

Have you tried hard coding $num as 0, then running the code. Then hard code it to equal 1, run code again. etc etc until you’ve tried all the possible values in your switch statement and see if you find an issue with a particular case. You could also change the end of your statement to this:

[php]case 2:
echo “third sentence…”;
break;
default:
echo “Default case has come into play!”;
}[/php]

Have had no issues testing your code.

Also included echo $num; and all was correct.

this code works perfectly on my server, I think the problem is with your server or your computer. I do not see any room for error in the code.

Sponsor our Newsletter | Privacy Policy | Terms of Service