← back to the blog

PHP casting and JavaScript coercion

Posted on September 28th, 2018 in PhP, JavaScript by George

PHP and JavaScript do not require explicit type definiton whenever you are creating a variable.The variable type is going to be defined by the context in wich the variable is assigned.Converting a data type using an explicit method it is called casting while converting a data type using an implicit (forced way) it called coercion.

PHP casting

  works this way. The name of the expected type it is writen betwen ( ) parentheses before the variable to be casted. In PHPyou can cast literal strings to binary strings.The cast allowed are:

(int) and (integer) - will cast to integer

(bool),(boolean) - will cast to boolean 

(float),(double),(real) -cast to real or float number

(string),(array),(object) are casting to they respective data type, string,array,object

and (unset) will cast to NULL (The (unset) cast will be removed as of PHP 8.0)



$testMe = 10;
// $foo is an integer
$casted = (boolean) $testMe;
// $bar is a boolean
echo 'Value now is = ' . $casted;
Value now is = 1


JavaScript coercion.

Javascript coerion will always result in one of scalar primitve values, string, number, boolean. Therefore is not possible to coerce into object or callable object(function).

Boxing a value in they object counterpart could be seen as explicit type casting of Javascript .

let c = 55;
let d = String(c);

console.log(typeof c);
console.log(typeof d);



For implicit coercion one can coerce a number to a string simply by "adding" the number and the empty string  ""  or  `` :

var a = 125;
var b = a + "";

b; // "125"  // string

 Substracting a number from a string is goin to result coercion into a number. But addition on the other hand is going to result into a type of string. This is pure example of a javascript 'unexpected' behaviour which can cause a lot of frustation. This is one of the things you have to be aware when you using implicit coercion.

var a = "3.14";
var b = a - 0;

console.log(typeof b,b);
//number 3.14
var c = '434';
let d = c + 5;

console.log(typeof d ,d);
//string 4345 ... do you observe
// string concatenation
//instead of addition?


Take this as a BE AWARE example

var a = 121;
var b = "red";
var c = null;

console.log(a || b);    // 121
console.log(a && b);    // "red"

console.log(c || b);    // "red"
console.log(c && b);    // null

In Php, those expressions result in true or false, but in JavaScript the result comes from the values themselves. If you have started by learning first Javascript and then you pick up to learn PHP (like me, as a matter of fact) you have to be aware of this type of behaviour and not just assume similar behaviour but read the documentation carefuly before steping into coding. 

There are more many other nitty-gritty things going on about implicit coercion but one thinks is for sure. NOTHING MAGIC is going on, everything is based on strict rules when it comes to inplicit coercion ('use strict;'). All you have to do is to be aware and test for the expected output before pushing into your program.