Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts

    inline echo sum of 2 variables possible?

    Hi Forum,

    i have this code (that works)

    but i want to add 2 vars together in a similar script

    PHP Code:

        
    foreach ($_SOURCE['O'] as $k => $v){
            if (
    $v >= 1){
                
    $sum $_SOURCE['E'][$k]-$_SOURCE['A'][$k]; // my workaround-----------------------------------------------------------------
                
    if ($sum >= 0){$totalcolour 'class="higher"';}else{$totalcolour 'class="lower"';}
            
    $Content .= '
                <tr>
                    <td>'
    .$ops[$k].' Estimate: <input type="hidden" name="O'.$k.'" value="'.$_SOURCE['O'][$k].'"></td>
                    <td><input type="text" name="Est'
    .$k.'" value="'.$_SOURCE['E'][$k].'"> </td>
                    <td>Actual: </td>
                    <td><input type="text" name="Act'
    .$k.'" value="'.$_SOURCE['A'][$k].'"></td>
                    <td>Difference: </td>
                    <td><input type="text" name="Dif'
    .$k.'" '.$totalcolour.' value="'.$sum.'"></td> // the value of this box------------
                </tr>
            '
    ;
            }else{
                
    $Content .= '
                <tr>
                    <td><a href="'
    .$_SERVER['PHP_SELF'].'?TasNO='.$_SOURCE['TasNO'].'&add='.$k.'">'.$ops[$k].'</a></td>
                    <td>N/A</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            '
    ;
            }
        } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'm not sure I understand your question, so I'll pretend I do and see if I get it right.
    +, -, and . all have the same precedence, and its evaluated from left to right. So if I concat inline with a + or - in it, the order changes implicitly:
    PHP Code:
    $i 4;
    $j 6;
    print 
    'A string with ' $i $j ' in it'
    Becomes implicilty:
    PHP Code:
    print ((('A string with ' $i) + $j) . ' in it'); 
    So the result of that would be:
    Code:
    6 in it
    Since 'A string. . .' cannot be interpreted as a number. More bizarre is what happens when you have a string that *can* be interpreted as a number:
    Code:
    print '2 dogs and ' . $i + $j . ' cats';
    becomes:
    Code:
    8 cats
    So the answer: as per any precedence control, wrap the appropriate operation in brackets to force the correct evaluation order (desired).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    thanks, that fixed it

    both are float entrys
    $_SOURCE['E'][$k] = 12.00
    $_SOURCE['A'][$k] = 0.00

    $sum = $_SOURCE['E'][$k]-$_SOURCE['A'][$k];
    echo $sum //shows 12.00

    echo $_SOURCE['E'][$k]-$_SOURCE['A'][$k]; // shows 0.00

    echo ($_SOURCE['E'][$k]-$_SOURCE['A'][$k]); // shows 12.00
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I can't replicate this problem you are seeing with the code you have posted. Those should all result in 12 as there is no order conflict to alter the evaluation. Are you sure they are actually floats and not strings?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •