How to do "+" symbol to be $y

Discussion in 'PHP' started by Adulu, Jun 22, 2009.

  1. #1
    <?php
    
    $y="+";
    
    $x=(10 $y 10); //$x=(10+10);
    echo $x;
    
    ?>
    PHP:
    above is my assume, of course it's error.

    i'd like to do a switch "+" "-"
    how to fix it, echo $x will be 20

    thank you
     
    Adulu, Jun 22, 2009 IP
  2. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try the following it will echo 20.

    <? 
    $x = "10";
    $y = "10";
    echo ($x + $y); 
    ?>
    Code (markup):
     
    myhart, Jun 22, 2009 IP
  3. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $y must be "+"; due to i 'd like to switch
    it mean i am just change "+" to be "-"
    $x will be 0
    if change "+" to be "*"
    $x will be 100
     
    Adulu, Jun 22, 2009 IP
  4. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    $y='+';
    $x=0;
    switch($y)
    {
    	case '+':
    		$x=10+10;
    		break;
    	case '*':
    		$x = 10 * 10;
    		break;
    }
    echo $x;
    ?>
    
    Code (markup):
     
    franklyn, Jun 22, 2009 IP
  5. uselessguy

    uselessguy Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try using eval function that can evaluate string as a php code

    $x=eval((10 $y 10));
    PHP:
     
    uselessguy, Jun 22, 2009 IP
  6. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thank you
    but it seems can't using in my case
    if($a+$b)
    {
    }
    PHP:
    i'd like to switch
    if($a+$b)
    {
    }
    to be below
    if($a-$b)
    {
    }
    just change + symbols
    but +- seems unable to be variable as far as i know.
     
    Adulu, Jun 23, 2009 IP
  7. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    $y='*';
    $x=eval((10 $y 10));
    echo $x;
    ?>
    i am tried it, but can't work.
     
    Adulu, Jun 23, 2009 IP
  8. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #8
    Eval is too heavy. There is a much simpler solution. Use a simple multiplier initialized with 1 (for adding) or -1 (for subtraction).
    Example:

    <?php
    
    if ($subtract)
     $mul = -1;
    else
     $mul= 1;
    
    $x = 10 + $mul*10;
    echo $x;
    
    ?>
    PHP:
     
    wmtips, Jun 23, 2009 IP
  9. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you're getting your cases mixed up , what you want to do is check $y and then based on that you change the operation appropriately.
     
    franklyn, Jun 23, 2009 IP
  10. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #10
    it should be $x=eval("10 $y 10");
     
    octalsystems, Jun 24, 2009 IP