DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Type Casting

The meaning of "Casting" is taking a variable of one particular data type and converting it to another data type. (ex: Integer to Float)

As we discussed in the variables chapter, in PHP, we do not need to specify the data type of the variable when we declare it. PHP parser automatically set it for us. But, the data type of the variable can be converted to another one automatically or manually. This is called Type Casting.

There are two types of casting

Implicit Casting

Implicit casting is something done by PHP according to your code, but you are not capable of customizing. When dividing an integer by another integer the result can either be integer or float. The decision is taken by PHP. See the below example.

PHP Implicit Casting Example


<?php
$x = 2; 
$y = 4;

var_dump($x / $y); // 2/4 = 0.5 (Float)
var_dump($y / $x); // 4/2 = 2 (Int)

Run Example ››

Explicit Casting

We can explicitly cast a variable to different data types. For instance, we can convert a float to an integer like following. (The decimal portion will be dropped)

PHP Explicit Casting - Float to Integer


<?php
$x = 5.35;
$y = (int) $x; // cast $x to integer
var_dump($y);

Run Example ››
The data type you need to change the variable into should be written inside parentheses, before the variable.

The following casts are allowed:

Cast Description
(int) or (integer) Cast to an integer.
(float) or (double) or (real) Cast to a float.
(bool) or (boolean) Cast to a boolean.
(string) Cast to a string.
(array) Cast to an array.
(object) Cast to an object.

Explicit Casting Examples

1. Casting to an integer

Both (int) and (integer) casts are valid.

When casting a float to integer, the decimal portion will be dropped. So, make sure that this drop does not affect any other things.

PHP Explicit Casting - Float to Integer


<?php
$x = 5.35;
$y = (int) $x; // cast $x to integer
var_dump($y);

Run Example ››

We can also cast strings to integers. This can be useful when collecting numeric data from forms. (More details in the PHP forms chapter)

PHP Explicit Casting - String to Integer


<?php
$x = '25';
$y = (integer) $x; // cast $x to int
var_dump($y);

Run Example ››

The following example shows a useful place that you can use casting for. Casting any string that starts with a number followed by a phrase to an integer will return the starting number as an integer.

PHP Explicit Casting - Phrase to Integer


<?php
$string = '10 Animals';
$numberOfAnimals = (int) $string;
echo $numberOfAnimals;

Run Example ››

2. Casting to a float

(float), (double) and (real) are valid casts. All of them casts the variable to float data type.

When casting an integer to float there won't be any data loss, as integers do not have any decimal portion.

PHP Explicit Casting - Integer to Float


<?php
$x = 7;
$y = (float) $x; // $y is 7, but float
var_dump($y);

Run Example ››

3. Casting to a boolean

Both bool and boolean are valid.

PHP Explicit Casting to Boolean


<?php
$a = (bool) 0; 
$b = (bool) 5; 
$c = (bool) '';
$d = (bool) 'Hyvor';
$e = (bool) [];
$f = (bool) [2,5];
$g = (bool) null;
var_dump($a); // false
var_dump($b); // true
var_dump($c); // false
var_dump($d); // true
var_dump($e); // false
var_dump($f); // true
var_dump($g); // false

$h = (boolean) 'Hyvor'; // boolean also valid
var_dump($h); // true

Run Example ››

4. Casting to an array

We can add any single variable into an array which has only one element.

PHP Explicit Casting to Array


<?php
$a = (array) 5;
$b = (array) 'Hyvor'; 
$c = (array) true;
var_dump($a); // [5]
var_dump($b); // ['Hyvor']
var_dump($c); // [true]

Run Example ››
0
180