Jump to content

Beginner help -- Adding a number with a variable


Jumbala102

Recommended Posts

Hi, I'm just starting PHP and I ran into a simple problem (that I can fix), but I just want to understand why my first approach doesn't work.

 

I was testing multiplications and additions and the result I got was unexpected.

 

Here is the code that doesn't work:

	<?php
	$number1 = 5;
	$number2 = 10;

	print $number1." multiplied by ".$number2." + 1000 = ".$number1*$number2+1000;
?>

 

Obviously, what I want as a result when I'm displaying the page is: "5 multiplied by 10 + 1000 = 1050"

 

What I get: "1005". That's right, nothing else. On top of getting an erroneous answer, the sentence that's supposed to display right before the equation doesn't display at all.

 

It's even worse if I switch it around like that:

 

	print $number1." multiplied by ".$number2." + 1000 = ".1000+$number1*$number2;

 

I get an error message and nothing displays.

 

The obvious solution works: using a parenthesis at the beginning and at the end of the equation, which I should do regardless, but still, it's bugging me and since I just started learning PHP today, I want to understand how the code is compiled / interpreted so I don't make stupid mistakes like this one.

 

Thanks in advance for the replies.

Link to comment
Share on other sites

Something like this should work the way you want it to:

 

<?php
$number1 = 5;
$number2 = 10;
$result = $number1*$number2+1000;

print $number1 . "multiplied by " . $number2 ."+ 1000 =" . $result;
?>

 

I'm also a beginner and as far as I understand you have to make the dots to seperate the strings from the text properly.

(example):

 

echo $string1 . $string2 . $string3 . "text1" . "text2" . $string4 . "text3";

 

This probably doesn't explain the whole story, someone who knows more than me will probably explain it way better.

Link to comment
Share on other sites

It's a concept called "operator precedence". In math, multiplication and division have equal precedence, but both have higher precedence than addition and subtraction. It's why 1+2*3=7 and not =9.

 

In PHP, multiplication also has higher precedence than addition, while string concatenation is of equal precedence with addition. Adding parentheses to make it more obvious, your statement looks like

print ($number1." multiplied by ".$number2." + 1000 = ".($number1*$number2)+1000);

Now there's another concept: "associativity". Multiplication, addition, and concatenation all have left-to-right associativity, which means stuff gets evaluated from left to right. With more parentheses,

(                                                                              +1000)
(                                                        .($number1*$number2))
  (                                         ." + 1000 = ")
   (                              .$number2)
    (          ." multiplied by ")
     ($number1)

The last piece of the puzzle is "loose typing". PHP will convert strings to numbers and vice versa depending on what you do to them. Multiplication and addition require numbers and string concatenation requires... well, strings. After collapsing that block of stuff above to the last two steps you get

"5 multiplied by 10 + 1000 = 50"+1000

As I said, addition requires numbers, so PHP converts that string into a number before using it. The result is 5. Thus

print 5+1000; // 1005

 

Besides breaking the expression into multiple statements, you can just add parentheses.

$number1." multiplied by ".$number2." + 1000 = ".($number1*$number2+1000);

Link to comment
Share on other sites

I'm so glad I wasn't a smartass by trying to answer the question - just because I couldn't explain it nearly as good, or couldn't explain it at all.

 

Great explanation requinix.

Link to comment
Share on other sites

It's a concept called "operator precedence". In math, multiplication and division have equal precedence, but both have higher precedence than addition and subtraction. It's why 1+2*3=7 and not =9.

 

In PHP, multiplication also has higher precedence than addition, while string concatenation is of equal precedence with addition. Adding parentheses to make it more obvious, your statement looks like

print ($number1." multiplied by ".$number2." + 1000 = ".($number1*$number2)+1000);

Now there's another concept: "associativity". Multiplication, addition, and concatenation all have left-to-right associativity, which means stuff gets evaluated from left to right. With more parentheses,

(                                                                              +1000)
(                                                        .($number1*$number2))
  (                                         ." + 1000 = ")
   (                              .$number2)
    (          ." multiplied by ")
     ($number1)

The last piece of the puzzle is "loose typing". PHP will convert strings to numbers and vice versa depending on what you do to them. Multiplication and addition require numbers and string concatenation requires... well, strings. After collapsing that block of stuff above to the last two steps you get

"5 multiplied by 10 + 1000 = 50"+1000

As I said, addition requires numbers, so PHP converts that string into a number before using it. The result is 5. Thus

print 5+1000; // 1005

 

Besides breaking the expression into multiple statements, you can just add parentheses.

$number1." multiplied by ".$number2." + 1000 = ".($number1*$number2+1000);

Wow, thank you very much for the detailed answer. It's really well explained.

 

Just a last question, though. While I do get that concatenation and additions have the same precedence order and that the way I typed my "print" code made it so it had to convert the string into a number, I just want to know how it figured that the string was equal to 5?

 

Other than that, I'm pretty sure I won't ever be making that kind of mistake in my scripts since I now understand how it works.

 

Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.