echo'ing in the a web browser

So I’m in the midst of taking the baby steps of learning PHP.
Following and testing the steps from a tutorial website I’m finding myself a bit miffed…
They are going through showing the simple assignment of numbers to variables and adding them.
When i tried their example the answer came out incorrect.

My question is “why?”

The output of the code below is:

9
13

[php]<?
$four = 4;
$five = 5;
$nine = $four + $five;

// My example
echo $nine , “
”;

// Their example
echo $four + $five = $nine , “
”;
?>[/php]

You can use this one (more simple):

[php]

<?php $four = 4; $five = 5; $nine = 9; echo $nine ."
"; echo $four ."+". $five ."=". $nine; ?>

[/php]

The reason is because some of the operators needed the concatenation operator and be enclosed in quotes.
the reason being they have a different meaning when not in quotes, when in quotes they are a string and do nothing.
When in the code
= equals

  • plus
  • minus
  • times

So .’+’. will be just that a char + and does nothing.

[php]<?php
$four = 4;
$five = 5;
$nine = $four + $five;

// My example
echo 'nine = '.$nine , “
”;

// Their example
echo $four .’+’. $five .’ = '. $nine , “
”;
?>[/php]

I have my answer. Although from the answers given, I see that I may not have explained myself very well (not the first time and probably wont be the last).
So my question was really about the answer 13. I didn’t understand where it was coming from.
In my code i have:

<? $four = 4; $five = 5; $nine = $four + $five; // Their example echo $four + $five = $nine , "
"; ?>

I’m not sure why the tutorial example i followed showed me to do it this way.
The intention here was not to create an equation string: “4 + 5 = 9”
The intended result here was to display the answer to $four + $five.
So I see that it would be better to just

echo $four + $five;

or

echo $nine;

However I still wondered why this “echo $four + $five = $nine , “
”;” would have an output of 13.
It looks like there is more studying I will need to do, but what it looks like is that there is a precedence in the operators.
The $five = $nine portion is actually equating the value of $nine to $five which in turn makes the problem $four + $nine which equals 13.
And this is not the intended answer.

So now it seems like the function of the “=” comes before the “+”.

Please correct me if i’m wrong but this is what it looks like to me

Sponsor our Newsletter | Privacy Policy | Terms of Service