Jump to content

[SOLVED] echo random phrase


webguy262

Recommended Posts

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!

Link to comment
Share on other sites

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];
?>

Link to comment
Share on other sites

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!";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.