echo question

Discussion in 'PHP' started by justinlorder, Mar 8, 2009.

  1. #1
    Hello, php gurus .
    I have a simple question in one of my small work .

    I want to echo some variable .
    eg.
    <?php echo $vara; ?>
    <?php echo $varb; ?>

    The problem is I want to random echo $vara or $varb .
    How to achieve it . Thanks .
     
    justinlorder, Mar 8, 2009 IP
  2. qualityhostings

    qualityhostings Well-Known Member

    Messages:
    1,764
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #2
    <?php
    srand(time());
    $random = (rand()%9);
    //This will store a random number between 0 and 9 into $random

    $vara='Hi';
    $varb='Hello';

    echo $random . "<br>" ;

    if(($random % 2 ) == 0 )
    echo $vara;
    else
    echo $varb;
    ?>
     
    qualityhostings, Mar 8, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    <?php
    $var = array(1=>"one", 2=>"two");
    $spin = rand(1,2);
    echo $var[$spin];
    ?>
    PHP:
     
    ActiveFrost, Mar 8, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Make it an array so that you can do that easily

    
    $var = array('a','b');
    echo $var[rand(0,1)];
    
    PHP:
    - ads2help
     
    ads2help, Mar 8, 2009 IP
  5. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your feedback . They work very well.
     
    justinlorder, Mar 12, 2009 IP