LiamProductions Posted August 4, 2007 Share Posted August 4, 2007 Hey. I made a script to randomize a slogan to echo out to the browser. But... instead of echoing the slogan line it echos the random number i was wondering whats wrong heres the script: <?php $slog1 = "Liam's website"; $slog2 = "Liam's production website"; $slog3 = "Quick loading website"; $slog4 = "Liam Can help you today"; $n = rand(1,4); echo "<p>$slog$n</p>"; ?> Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/ Share on other sites More sharing options...
timlondon Posted August 4, 2007 Share Posted August 4, 2007 Should it be echo $slog.$n ?? Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315508 Share on other sites More sharing options...
LiamProductions Posted August 4, 2007 Author Share Posted August 4, 2007 No the number is trying to pick like slog1... and i tried the . in the middle and it just prints out .4 [it echos out a random number] Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315509 Share on other sites More sharing options...
timlondon Posted August 4, 2007 Share Posted August 4, 2007 What about: $new = "slog".$n; echo $new; Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315512 Share on other sites More sharing options...
timlondon Posted August 4, 2007 Share Posted August 4, 2007 Put it into an array: $slog[1] = "Liam's website"; $slog[2] = "Liam's production website"; $slog[3] = "Quick loading website"; $slog[4] = "Liam Can help you today"; $n = rand(1,4); echo $slog[$n]; Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315513 Share on other sites More sharing options...
LiamProductions Posted August 4, 2007 Author Share Posted August 4, 2007 that just echo's out slog.number [number being the random one] Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315514 Share on other sites More sharing options...
LiamProductions Posted August 4, 2007 Author Share Posted August 4, 2007 Put it into an array: $slog[1] = "Liam's website"; $slog[2] = "Liam's production website"; $slog[3] = "Quick loading website"; $slog[4] = "Liam Can help you today"; $n = rand(1,4); echo $slog[$n]; Thanks, got it working now Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315516 Share on other sites More sharing options...
wildteen88 Posted August 4, 2007 Share Posted August 4, 2007 I know this is solved, but Just to let you know in order to get your last setup to work you'd do this: echo '<p>' . ${'slog' . $n} . '</p>'; The technique is called variable variables. Link to comment https://forums.phpfreaks.com/topic/63314-solved-little-problem/#findComment-315518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.