1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help me solve an easy php question

Discussion in 'PHP' started by justinlorder, Dec 30, 2008.

  1. #1
    I need the php code in one of my design .

    There are two constants in a php file .
    One is named "con1" and the other is named "con2" .
    There are also two variables, "var1" and "var2" .
    I want to set the two variables to the two constant value .

    when var1 = con1 , then var2 = con2 .
    when var1 = con2 , then var2 = con1 .

    One variable is corresponding to one constant , but they are randomly correspond to one constant .
    Two variables can't be the same constant !

    How can I achieve it with shortest php code ?
    Plus, do you have to define the variables before I use them ?
    Thanks in advance ! :D
     
    justinlorder, Dec 30, 2008 IP
  2. champ

    champ Member

    Messages:
    30
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #2
    ternary operator:
    $var2 = ($var1 == con1) ? con2 : con1;
    PHP:
     
    champ, Dec 30, 2008 IP
  3. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #3

    There are two situations .

    when var1 = con1 , then var2 = con2 .
    when var1 = con2 , then var2 = con1 .

    I need randomly to set a constant value to the variables .

    $var2 = ($var1 == con1) ? con2 : con1;
    Did you set var1 to con1 ? How about another situation ?
     
    justinlorder, Dec 30, 2008 IP
  4. champ

    champ Member

    Messages:
    30
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #4
    After var1 is set to con1 the ternary operator makes sure that var2 will always be the other constant. It doesn't care which constant var1 was set to as it will always choose the other one.
     
    champ, Dec 30, 2008 IP
  5. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I need to set var1 randomly . That is to say, var1 may be set to con1 and con2 . The code you gave set var1 to con1 only . How about the other situation ?
     
    justinlorder, Jan 1, 2009 IP
  6. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can use the random function, rand() to introduce the randomness

    $i = rand(1,2);
    $var1 = "con"$i;
    $var2 = ($var1 == con1) ? con2 : con1;
     
    fairuz.ismail, Jan 1, 2009 IP
  7. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks . I really appreciate you both .
    Actually, I take "con1" and "con2" as example. The two constant may be "constantA" and "constantB" and others .
    How to write the php code to achieve it ?
     
    justinlorder, Jan 1, 2009 IP
  8. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $i = rand(1,2);
    if($i == 1){
    $var1 = con1;
    $var2 = con2;
    }
    else{
    $var1 = con2;
    $var2 = con1;
    }
     
    fairuz.ismail, Jan 1, 2009 IP
    justinlorder likes this.
  9. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #9
    fairuz.ismail
    I appreciate your help . Your php code solve my problem . Rep point is given !
     
    justinlorder, Jan 1, 2009 IP