Type casting in PHP

PHP does not support explicit type definition in variable declaration. For example, you CANNOT declare a variable as follows:

int $count = 10;

In PHP, a variable's type is determined by the context in which the variable is used.

$student = "23";    // $students is a string
$girls = 13;         // $girls is an int
$boys = $students - $girls;    // all three variables are converted to int.

There are many scenarios when automatic typecasting could lead to erroneous functionality. In such cases, you can manually cast the variables.

$count = 1;     // $count is an int but I need it to be an boolean
$count = (bool) $count;    // $count is a boolean now

Following casts are allowed in PHP:

binary will cast to binary
bool, boolean will cast to boolean
float, double will cast to float
int, integer will cast to integer
string will cast to string
array and object can also be used for casting
Technologies: 
Actions: