Tag Archives: Type

Type Coercion

In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

Type Coercion means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.

<?php
 $phpcodez= 10;   // $phpcodez is an integer
 $bar = (boolean) $phpcodez;   // $phpcodez is a boolean
 ?>
Share

Type Juggling

Type Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

<?php
 $phpcodez = "1"; // $phpcodez is string (ASCII 49)
 $phpcodez *= 2; // $phpcodez is now an integer (2)
 $phpcodez = $phpcodez * 1.3; // $phpcodez is now a float (2.6)
?>
Share