PDA

View Full Version : Simple PHP help needed


shifty.ricky
28-03-2003, 22:58
HI Guys,

<?php
switch(mt_rand(0,2)) {
case 0: print "link1";
case 1: print "link2";
case 2: print "link3";
}
?>

I am signed up with a number of affiliate schemes and i am using (trying to) get this to randomly display which one is shown but it gives this error

P**** error: p**** error, unexpected T_STRING in /home/collyer/public_html/front.php on line 23

line 23 is the case 0 line. I know php works as i have php bb on the server working.

Any help appreciated..

Edit: worked out its becuase the links have a " in...any ideas?

Edit2: ok i have used the escape charater to stop the errors but now the script will print link1, or link 1 and 2, or link 1 and 2 and 3

It seems that if the case is 2 then it is printing 1 and 2 not just 2 any ideas on this.

Cheers
Richard

Kynoch
28-03-2003, 22:59
link1 doesnt have an " after it.

shifty.ricky
28-03-2003, 23:11
Ooops...that was a error copying to the board...sorry

It does in the real script.

Kynoch
28-03-2003, 23:17
ah well thats the only help i can offer, i aint in for PHP yet.

ling_thing
28-03-2003, 23:28
how abotu this instead

not as eligant
but it works


<?php

$test = mt_rand(0,2);

if ($test == 0)
echo "link1";
else if ($test == 1)
echo "link2";
else if ($test == 2)
echo "link3";
?>


<?php

$test = mt_rand(0,2);

if ($test == 0)
print "link1";
else if ($test == 1)
print "link2";
else if ($test == 2)
print "link3";

?>

AlastairM
28-03-2003, 23:31
ok, try:


switch(mt_rand(0,2)){

case 0:
print ("link1");
break;

case 1:
print("link2");
break;

case 2:
print("link3");
break;
}


It seems that PHP doesn't 'short' the switch statement when the condition is met, unlike ASP does, so you need to add a break after each option if you want it to exit.

cheers

alastair

shifty.ricky
28-03-2003, 23:32
Cheers for the speedy responses guys. Thanks very much its appreciated.