Type casting changes a value from one type to another. It helps when you want to treat data in a specific way.
Type casting syntax in PHP
(type) $variable;
Replace type with the desired type like int, float, string, bool, array, or object.
Put the type in parentheses before the variable you want to convert.
$num = (int) "123";false.$flag = (bool) 0;$price = (float) "12.99";$obj to an array.$arr = (array) $obj;This program shows how to convert a string to float, then to int, then back to string, and finally to boolean. It prints each step to see the changes.
<?php // Original string $priceString = "19.99"; // Convert string to float $priceFloat = (float) $priceString; // Convert float to int $priceInt = (int) $priceFloat; // Convert int to string $priceStr = (string) $priceInt; // Convert int to boolean $isAvailable = (bool) $priceInt; // Print all values print("Original string: $priceString\n"); print("Float value: $priceFloat\n"); print("Integer value: $priceInt\n"); print("String value: $priceStr\n"); print("Boolean value: " . ($isAvailable ? 'true' : 'false') . "\n"); ?>
When casting to int, decimals are removed (not rounded).
Casting to bool makes zero, empty strings, and empty arrays become false, everything else is true.
Be careful when casting objects to arrays and vice versa, as it changes how you access data.
Type casting changes data from one type to another using (type) before a variable.
Common types are int, float, string, bool, array, and object.
Use type casting to make sure your data works the way you want in your program.
Practice
$num = (int) "123";Solution
Step 1: Identify the type casting operation
The code uses (int) before a string variable, which means it converts the string to an integer.Step 2: Understand the conversion result
The string "123" becomes the integer 123 after casting.Final Answer:
Converts the string "123" to an integer 123 -> Option AQuick Check:
Type casting (int) converts string "123" to integer 123 [OK]
- Thinking it converts integer to string
- Expecting a syntax error
- Assuming the variable stays a string
$value to a float in PHP?Solution
Step 1: Recall PHP type casting syntax
PHP uses (type) before a variable to cast it, so (float) $value is correct.Step 2: Check each option
$floatValue = float($value); uses a function-like syntax which is invalid. $floatValue = cast float $value; uses invalid syntax. $floatValue = (float)$value[]; tries to cast an array element incorrectly.Final Answer:
$floatValue = (float) $value; -> Option AQuick Check:
Correct cast syntax is (float) $value [OK]
- Using function call syntax like float($value)
- Writing cast without parentheses
- Adding invalid array brackets after cast
$a = "10 apples";
$b = (int) $a;
echo $b;
Solution
Step 1: Understand casting string with numbers and text
When casting a string starting with numbers to int, PHP converts the leading number part only.Step 2: Apply casting to the string "10 apples"
The string starts with "10", so (int) "10 apples" becomes integer 10.Final Answer:
10 -> Option CQuick Check:
String starting with digits casts to those digits as int [OK]
- Expecting full string output
- Thinking casting fails with text
- Assuming zero because of text
$value = "123.45";
$intValue = int($value);
echo $intValue;
Solution
Step 1: Check the casting syntax used
The code uses int($value), which looks like a function call but PHP does not have int() function for casting.Step 2: Identify correct casting method
PHP uses (int) $value to cast, not int($value).Final Answer:
int() is not a valid cast syntax in PHP -> Option BQuick Check:
Use (int) for casting, not int() function [OK]
- Using int() like a function
- Confusing casting with function calls
- Ignoring PHP casting syntax rules
$data = ["5", "3.14", "true", "false", "0"];, which PHP code correctly casts each element to its proper type (int, float, bool) using type casting syntax and stores results in $result?Solution
Step 1: Analyze required casts for each element
"5" should be int, "3.14" float, "true" and "false" to bool, "0" to bool.Step 2: Check each option for correct casting syntax and types
$result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; uses correct (type) syntax and casts each element properly. $result = [int($data[0]), float($data[1]), bool($data[2]), bool($data[3]), bool($data[4])]; uses invalid function-like casts. $result = [(int)$data[0], (int)$data[1], (bool)$data[2], (bool)$data[3], (int)$data[4]]; casts "3.14" to int incorrectly. $result = [(float)$data[0], (float)$data[1], (string)$data[2], (string)$data[3], (bool)$data[4]]; casts "true" and "false" to string incorrectly.Final Answer:
$result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; -> Option DQuick Check:
Use (type) syntax and correct types for each element [OK]
- Using function calls instead of casting syntax
- Casting floats as int incorrectly
- Casting booleans as strings
Ready to prove it?
Pass the test and this pattern moves unknown
