Simple PHP Prob

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ricky
Forum Newbie
Posts: 2
Joined: Fri Mar 28, 2003 4:10 pm

Simple PHP Prob

Post by Ricky »

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

Parse error: parse 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try something like

Code: Select all

<?php
switch(mt_rand(0,2)) {
	case 0:
		print '<a href="link1" ....';
		break;
	case 1:
		print '<a href="link2" ....';
		break;
	case 2:
		print '<a href="link3" ....';
		break;
	default:
		print '<a href="default_link" ....'; // shouldn't happen 
}
?>
Ricky
Forum Newbie
Posts: 2
Joined: Fri Mar 28, 2003 4:10 pm

Post by Ricky »

thnaks for that it worked a treat.
Post Reply