I'm trying to add 2 to the $price each time the variables below isset (contrarily, I don't want to add 2 to the $price for any variable that is empty) . I have pasted my current sample code below. It works with the first statement by itself, but when I add more than one if statement it causes an error. Please advise what the best way to handle this is? I'm sure it's very simple...but I'm missing something. Still a beginner. Thank you in advance for the help!

<?php
                $price = 135;
?>

<?php
        if (isset($field_Charger)) {
                $price = $price + 2;
        }
        if (isset($field_Case)) {
                $price = $price + 2;
        }
        if (isset($field_Software)) {
                $price = $price + 2;
        }
        if (isset($field_Manual)) {
                $price = $price + 2;
        }
        if (isset($field_Box)) {
                $price = $price + 2;
?>

<?php
        echo $price;
?>

    The only problem I see in the code you posted is that your last if() statement is missing a '}' to match with it's '{' brace.

    Also note that by utilizing PHP's variable variables language construct (see: [man]variables.variable[/man]), you could shorten that code to something like:

    $variables = array( 'field_Charger', 'field_Case', 'field_Software', 'field_Manual', 'field_Box' );
    $price = 135;
    
    foreach( $variables as $var )
        if( isset( $$var ) )
            $price += 2;
    
    echo "Price is: $price";
      Write a Reply...