Type Casting in PHP

The expectations of an arithmetic expression involving two variables that are either integers or floating point numbers are straightforward. We expect the sum/product/etc. as a result. However, since PHP is dynamically typed, a variable involved in an arithmetic expression could be anything, including a Boolean, object, array, or string. When a Boolean is involved in an arithmetic expression, true is treated as 1 and false is treated as zero. If an array is involved in an expression, it is usually a fatal error.1 Non-null objects are treated as 1 and null is treated as 0. However, when string values are involved in an arithmetic expression, PHP does something called type juggling. When juggled, an attempt is made to convert a string variable into a numeric value by parsing it. The parsing goes over each numeric character, converting the value to a numeric type (either an integer or floating point number). The first type a non-numeric character is encountered, the parsing stops and the value parsed so far is the value used in the expression.

Consider the examples in Code Sample 1 below. In the first segment, $a is type juggled to the value 10, then added to 5, resulting in 15. In the second example, $a represents a floating point number, and is converted to 3.14, the result of adding to 5 is thus 8.14. In the third example, the string does not contain any numerical values. In this case, the parsing stops at the first character and what has been parsed so far is zero! Finally, in the last example, the first two characters in $a are numeric, so the parsing ends at the third character, and what has been parsed so far is 10. Relying on type juggling to convert values can be ugly and error prone. You can write much more intentional code by using the several conversion functions provided by PHP. For example:

$a = intval("10");
$b = floatval("3.14");
$c = intval("ten"); //c has the value zero

Code Sample 1:

$a = "10";
$b = 5 + $a; //b = 15

$a = "3.14";
$b = 5 + $a; //b = 8.14

$a = "ten";
$b = 5 + $a; //b = 5

//partial conversions also occur:
$a = "10ten";
$b = 5 + $a; //b = 15

There are several utility functions that can be used to help determine the type of variable. The function is_numeric($x) returns true if $x is a numeric (integer or floating point number) or represents a pure numeric string. The functions is_int($x) and is_float($x) each return true or false depending on whether or not $x is of that type.

$a = 10;
$b = "10";
$c = 3.14;
$d = "3.14";
$e = "hello";
$f = "10foo";

is_numeric($a); //true
is_numeric($b); //true
is_numeric($c); //true
is_numeric($d); //true
is_numeric($e); //false
is_numeric($f); //false

is_int($a); //true
is_int($b); //false
is_int($c); //false
is_int($d); //false
is_int($e); //false
is_int($f); //false

is_float($a); //false
is_float($b); //false
is_float($c); //true
is_float($d); //false
is_float($e); //false
is_float($f); //false

A more general way to determine the type of a variable is to use the function gettype($x) which returns a string representation of the type of the variable $x. The string returned by this function is one of the following depending on the type of $x: “boolean” , “integer” , “double” , “string” , “array” , “object” , “resource” , “NULL” , or “unknown type”.

Other checker functions allow you to determine if a variable has been set, if its null, “empty” etc. For example, is_null($x) returns true if $x is not set or is set, but has been set to null . The function isset($x) returns true only if $x is set and it is not null . The function empty($x) returns true if $x represents an empty entity: an empty string, false , an empty array, null, "0" , 0, or an unset variable. Several examples are presented in the table below.

Results for various variable values

Results for various variable values

String Concatenation

Strings in PHP can be concatenated (combined) in several different ways. One way you can combine strings is by using the concatenation operator. In PHP the string concatenation operator is a single period.

$s = "Hello";
$t = "World!";
$msg = $s . " " . $t; //msg contains "Hello World!"

Another way you can combine strings is by placing variables directly inside a string. The variables inside the string are replaced with the variable’s values. Example:

$x = 13;
$name = "Starlin";
$msg = "Hello, $name, your number is $x";
//msg contains the string "Hello, Starlin, your number is 13"

Licenses and Attributions

Speak Your Mind