Aim
To perform explicit type casting of PHP variables between scalar and compound types — string, int, float, bool, array and object — and verify the exact conversion rules the engine applies.
Theory
PHP is dynamically typed: a variable's type is whatever its current value is, and the engine freely performs type juggling (implicit conversion) inside expressions. Explicit casting overrides this with a cast operator placed before the value: (int), (float), (string), (bool), (array), (object).
Conversion rules exercised by this practical:
- String → int: PHP parses the leading numeric portion and stops at the first invalid character, so
(int) "123.45"yields123— the fraction is truncated, never rounded. - String → float: full numeric parse —
(float) "123.45"gives123.45. - → bool: exactly these values become
false:0,0.0,"","0",null,[]. Everything else — including"0.0"and"false"— becomestrue. - Scalar → array: the value is wrapped as element
[0 => value]. - Associative array → object: produces a
stdClasswhose properties are the array keys, read with the->operator. - Related tooling:
settype($var, "integer")converts in place;gettype()/var_dump()inspect types;intval(),floatval(),strval()are the function forms;declare(strict_types=1)switches scalar parameter coercion off so mismatches throwTypeError.
Requirements
- XAMPP with PHP 8.x, or the PHP CLI
- VS Code (or any editor), browser or terminal
Procedure
- Start Apache in the XAMPP Control Panel.
- Save the snippet as
p03_type_casting.phpinsideC:\xampp\htdocs\wbplab. - Open
http://localhost/wbplab/p03_type_casting.phpin the browser (optionally wrap output in<pre>for clean formatting) or runphp p03_type_casting.php. - Experiment: cast
"12abc","abc","0"and3.99to different types, predicting each result before running.
Explanation of the Code
$numString = "123.45"is the source value.$asInt = (int) $numStringtruncates to123, while$asFloat = (float) $numStringpreserves123.45.$asString = (string) $asIntconverts the integer back to the text"123"— the round trip has permanently lost the decimal part.$asBool = (bool) $boolStringcasts"1"totrue; the ternary prints the wordtruebecauseechowould otherwise render boolean true as a bare1.$asArray = (array) $asIntproduces[0 => 123], displayed withprint_r().$asObject = (object) ["name" => "Aman", "age" => 20]builds astdClass; the interpolated string reads its members as$asObject->nameand$asObject->age.
Expected Output
Original string: 123.45Cast to int: 123Cast to float: 123.45Int back to string: 123String to bool: trueInt to array: Array ( [0] => 123 )—print_rspreads this over several linesArray to object sample: Aman, 20
🎯 Viva Questions
- What does
(int) "123.45"return?123— leading numeric parse followed by truncation toward zero. - List the values that cast to
false.0,0.0,"","0",null,[]. - Casting vs
settype()? A cast returns a converted copy;settype()mutates the variable in place. - Which class does
(object)create?stdClass, PHP's generic empty class. - Type juggling vs type casting? Juggling is implicit engine conversion inside expressions; casting is programmer-forced.
- What does
declare(strict_types=1)change? Scalar type declarations stop coercing; a wrong argument type throws aTypeError.
CO Mapping
CO1, CO2