Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes Integers

Calculation strings with integers

I have done the javascripts basics course before starting the PHP one. One major difference is in calculation. Example Javascript:

var number = "1"; var numberTwo = 1; var outcome = number + numberTwo;

Var outcome will be 11. in PHP

$number = "1"; $number_two = 1; $outcome = $number + $number_two;

$outcome will be 2.

What's the difference? Is it PHP settings in your server? What is the reason it does work in PHP and not in Javascript?

3 Answers

Since one of your variables is a string and the other an integer, when you do

number + numberTwo

in javascript, you are actually concatenating the two variables because the plus-sign is the operator used for concatenation.

In php however, the period-sign is the concatenation operator while the plus-sign is the addition operator. So basically your + means two different things in javascript and php, hence why you get the different results.

Thank you!

I should add that in javascript the +-sign is used both for concatenation and addition. If both variables are integers (or in another numeric type), it will add. But if there is a string in there, it will concatenate.

Oh, and mark the question as solved, if it is :)

i would like to mark it as solved, but can't quite figure out how.