add number with string concatination problem

Discussion in 'PHP' started by naziasaman, Nov 8, 2009.

  1. #1
    1). I have 4 numbers to add up and display result with a string concate to them

    For example
    $first_number = 10;
    $second_number = 20;
    $third_number = 30;
    $fourth_number = 40;
    $direct_text = 'addition of 4 numbers is = ';

    I have to display

    Print(‘addition of 4 numbers is =’. $first_number + $second_number +$third_number + $fourth_number);

    But the concated sing is not displayed also the last variable ($first_number) is not added to string I mean resuld should like this

    addition of 4 numbers is = 100

    but result is

    90

    neither the prefixed string nor the last value after dot oprator is added in the result


    If I remove the concated string and dot then the addition is correct I mean correct result

    Also when I do the add operation in a variable and then concate it to the string then the result is correct

    I mean when the is like this

    $first_number = 10;
    $second_number = 20;
    $third_number = 30;
    $fourth_number = 40;
    $total = $first_number + $second_number +$third_number + $fourth_number;
    $direct_text = 'addition of 4 numbers is = ';
    print($direct_text . $total);


    then result is as required. I mean it is


    addition of 4 numbers is = 100

    but I want to know that why when we do addition in the same code where concate the string then it not work properly

    kindly let me know
     
    naziasaman, Nov 8, 2009 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Use this;
    Print('addition of 4 numbers is ='. ($first_number + $second_number + $third_number + $fourth_number));
    PHP:
     
    Sky AK47, Nov 8, 2009 IP
  3. naziasaman

    naziasaman Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for reply it is really working now.

    But i want to know the reason why the number are not added correctly and not displaying string when not enclosing in the parenthesis i just want to know the logic behind it
     
    naziasaman, Nov 8, 2009 IP
  4. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

    Example:

    <php
    $var = 3;

    echo "Result: " . $var + 3;
    ?>

    The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

    To print "Result: 6", use parantheses to alter precedence:

    <php
    $var = 3;

    echo "Result: " . ($var + 3);
    ?>
     
    fairuz.ismail, Nov 8, 2009 IP
  5. naziasaman

    naziasaman Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks so much i now completly solve the issue as well get the reason of error ur answer are very informative thanks again
     
    naziasaman, Nov 8, 2009 IP
  6. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    no problem =)
     
    fairuz.ismail, Nov 8, 2009 IP