Tag Archives: Coercion

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
 ?>