1. Computing & Technology

Number Strings

From , former About.com Guide

Just about all web sites that use PHP on the server also have JavaScript to run in the browser. Many sites using JavaScript in the browser use PHP on the server. The number of people using one of these languages who also use the other language is therefore reasonably high.

There are lots of similarities between these two languages which make learning one when you already know the other a lot easier. There are also many differences between the two languages including many obvious ones (such as the fact that PHP has classes and JavaScript only has objects) and some not so obvious ones. It is the not obvious differences between the two languages that are most likely to catch you out.

One of these not obvious differences is the way that the two languages handle numbers that are contained in text strings.

Both JavaScript and PHP are loosely typed languages which means that a value contained in a field that is of one type can easily be translated into any other type that can hold the value. Numbers can easily be converted to text strings since text strings can hold numbers. A text string can easily be converted to a number provided that the content of the string is a value that makes sense as a number.

One not obvious area where these two languages differ is how they treat numbers contained in text strings. JavaScript uses + as the concatenation operator for joining text strings together. Where the field on either side of the + operator is a string JavaScript converts the other field to a string and concatenates (joins) the strings together. PHP uses . (a period) as the concatenation operator and so + only gets used for adding numbers together. So if we have "3" + 7 we will get "37" in JavaScript and 10 in PHP. To get the same result in JavaScript as we get in PHP we have to force JavaScript to convert the string into a number by doing something like (+"3") + 7.

The fact that the two languages use a different symbol for their string concatenation operators means that this difference between the two languages is at least reasonably obvious. Where it becomes a lot less obvious is when we look at comparing two strings. Consider "2" == "2.00". Here we have two strings being compared and so JavaScript has no need to convert anything to a different type before doing the comparison. The two strings are of different lengths and so there is no way that JavaScript will ever consider them to be equal. Even if we replace the "2" with a 2 so that we are comparing a number with a string instead of two strings all JavaScript will do is to convert the number to a string in order to do the compare. The only way to get JavaScript to consider these two values to be equal is to force JavaScript to convert them both to numbers. Once we convert "2.00" to a number we no longer have anything to tell us that the number is supposed to have two decimal places and of course 2 == 2.

PHP treats "2" == "2.00" completely differently because PHP will automatically convert strings that contain numbers into those numbers whenever it can unless you specifically tell it not to. While a comparison between these two strings is false in JavaScript it is true in PHP because PHP recognises that the two strings we are comparing both contain numbers and so PHP compares them as numbers rather than comparing them as text strings.

It is the subtle differences such as this that are the things most likely to catch out someone who knows one of these languages well but who is just a beginner with the other language.

In this particular instance the way that PHP treats the numbers is the more intelligent way in terms of giving the answer that people are more likely to expect however it shows less intelligence on the part of the people responsible for setting PHP up to work that way since it made it inconsistent with the way that JavaScript was already set up to work and many of the people who would be learning PHP would already know JavaScript.

©2012 About.com. All rights reserved.

A part of The New York Times Company.