webguy262 Posted June 27, 2009 Share Posted June 27, 2009 Trying to learn so I would like to know why this does not work: <?php $phrase1="Relax ... Listen To The Sound Of Your Heartbeat"; $phrase2="Celebrate ... Life's Special Moments"; $phrase3="Connect ... Let Two Become One"; $phrase4="Remember ... Create Memories That Last A Lifetime!"; $rnd = rand(1,4); $line = $phrase . $rnd; echo $line; ?> Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/ Share on other sites More sharing options...
.josh Posted June 27, 2009 Share Posted June 27, 2009 because the dot is a string concatenater. So you are trying to append the value of $phrase to the value of $rnd. Since there's no $phrase var, you wind up with assigning 1,2,3 or 4 to $line. Anyways, you should be using an array instead. <?php $phrase[]="Relax ... Listen To The Sound Of Your Heartbeat"; $phrase[]="Celebrate ... Life's Special Moments"; $phrase[]="Connect ... Let Two Become One"; $phrase[]="Remember ... Create Memories That Last A Lifetime!"; shuffle($phrase); echo $phrase[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/#findComment-864808 Share on other sites More sharing options...
webguy262 Posted June 27, 2009 Author Share Posted June 27, 2009 Thanks... How does your suggestion compare with this approach (other than being more compact)? <? $num = Rand (1,4); switch ($num) { case 1: echo "Relax ... Listen To The Sound Of Your Heartbeat"; break; case 2: echo "Celebrate ... Life's Special Moments"; break; case 3: echo "Connect ... Let Two Become One"; break; case 4: echo "Remember ... Create Memories That Last A Lifetime!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/#findComment-864813 Share on other sites More sharing options...
Alex Posted June 27, 2009 Share Posted June 27, 2009 Besides missing a break; that looks fine. But the other way would be advised. Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/#findComment-864815 Share on other sites More sharing options...
Daniel0 Posted June 27, 2009 Share Posted June 27, 2009 The difference lies in that you can generate an array during runtime whereas a switch statement is created at compile time. If you are only ever going to use those four, it would technically speaking be better because you don't need to allocate memory for all the other strings. Surely you can see the portability and scaling issues though. However, do note that array indices start at 0 and not 1. It has to do with the internal representation of arrays. It simply makes more sense to start from 0. If you ever learn a language like C you'll know why. Besides missing a break; that looks fine. But the other way would be advised. Nothing is missing. Break is optional, and in particular with the last case doesn't it matter. It breaks implicitly at the end of the block anyway. Of course you might argue that it's better style to add it anyway, but that's an entirely different matter. @CV: I would imagine it's way faster generating one pseudo-random number than shuffling an array and selecting the new first element. Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/#findComment-864816 Share on other sites More sharing options...
webguy262 Posted June 27, 2009 Author Share Posted June 27, 2009 Excellent, guys! This board is the best source of php help I have come across, and the only way for a self-taught amateur to go beyond copying ans pasting from tutorials without know what's behind the code. Quote Link to comment https://forums.phpfreaks.com/topic/163916-solved-echo-random-phrase/#findComment-864829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.