...

View Full Version : simple random problem



firecat318
09-15-2009, 12:46 AM
Here is my code, its simple, I want php to generate a random number, then if its inbetween 1-5 echo a certain thing, or else if its 6-10 echo something else.



<?
$rand = rand(1,10)
if($rand == 1 || 2 || 3 || 4 || 5)
{
echo "<h1>one thing</h1>";
}

if($rand == 6 || 7 || 8 || 9 || 10)
{
echo "<h1>other thing</h1>";
}
?>


all that happens now is a blank page
thanks i'm dumb :thumbsup:

[vengeance]
09-15-2009, 12:54 AM
<?
$rand = rand(1,10);
if($rand == 1 || 2 || 3 || 4 || 5)
{
echo "<h1>one thing</h1>";
}

if($rand == 6 || 7 || 8 || 9 || 10)
{
echo "<h1>other thing</h1>";
}
?>


You missed a semi colon. But try this. This code can probably be optimized, though.



<?php
$rand = rand(1,10);

if($rand >= 1 && $rand <= 5){ // If $rand is greater than or equal to 1 AND less than or equal to 5
echo "Numbers 1 - 5!";
}

if($rand >= 6 && $rand <= 10){ // If $rand is greater than or equal to 6 AND less than or equal to 10
echo "Numbers 6 - 10!";
}
?>

firecat318
09-15-2009, 01:01 AM
thanks it worked



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum