[new learner issue] multiple if statements and the && operator help please

Discussion in 'PHP' started by Herpsy, Feb 3, 2012.

  1. #1
    if(is_numeric($_REQUEST[credit])) && if($_REQUEST[credit] == 1) && if($result[cash]<=$free_credit_trigger) $can_reset = true;
    PHP:
    Parse error: syntax error, unexpected T_BOOLEAN_AND in [path] on line X

    so I tried to wrap the Ifs in another bracket

    if((is_numeric($_REQUEST[credit])) && if($_REQUEST[credit] == 1) && if($result[cash]<=$free_credit_trigger)) $can_reset = true;
    PHP:
    Parse error: syntax error, unexpected T_IF in [path] on line X

    I tried adding { } as well, which I know is optional in some simple circumstances. I am assuming the first example is the closest im getting. Ive been trying for an hour. Please help


     
    Herpsy, Feb 3, 2012 IP
  2. Herpsy

    Herpsy Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    43
    #2
    *FACEPALM*

    if((is_numeric($_REQUEST[credit])) && ($_REQUEST[credit] == 1) && ($result[cash]<=$free_credit_trigger)) $can_reset = true; 
    PHP:
    I feel like an idiot. thanks anyway. hahaha
     
    Herpsy, Feb 3, 2012 IP
  3. FizixRichard

    FizixRichard Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You dont nest IF's within the IF itself. I also don't think you even need to compound your statements in that IF statement at all.


    This is the correct code for what you have written
    
    if(is_numeric($_REQUEST[]) && $_REQUEST['credit'] == 1 && $result['cash'] <= $free_credit_trigger)
    {
       $can_reset = true;
    }
    
    PHP:
    Brackets like you've done is for compounding statements, something like this:

    
    if(($var1 == "something" || $var1 == "somethingElse") && ($var2 >= '1' && $var2 <= '10))
    {
    }
    
    PHP:
    So you can have multiple, compounded conditions. I don't see the need to compound the statements like you have there.
     
    FizixRichard, Feb 3, 2012 IP
  4. FizixRichard

    FizixRichard Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also

    
    $result[cash]
    
    PHP:
    Is semantically incorrect.

    
    $result['cash']
    
    PHP:
    Is correct, while PHP will allow you to ommit the '' from the array you should include it else you can encounter problems later.
     
    FizixRichard, Feb 3, 2012 IP