PHP Type Casting
Type casting is converting an expression of a given one type into another type. It is denoted by (type). It is used to convert lower data type to higher data type to avoid data loss.
There are two types of type casting:
- Implicit conversion
- Explicit conversion

Syntax:-
(type)value;
Implicit conversion:
It does not require any operator for converted. They are automatically performed when a value is copied to a consistent type in the program.
The value has been promoted from int to double and we have not to specify any type-casting operator. This is known as standard conversion.
Example:-
<?php $i = 20; $p = $i; // Implicit casting from int to float (handled automatically in PHP) echo "Implicit value is " . $p . "\n"; ?>
Explicit conversion:
Many conversions that involve a different interpretation of the value require an explicit conversion.
They are not automatically performed. It need to give explicit type casting in the program.
Example:-
<?php $i = 9; $j = 2; $z = (float) $i / $j; // Explicit casting from int to float echo "Value of Z : " . $z . "\n"; ?>

If the float is converted to int, data which is present after the decimal point will be lost.
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com