Consider the following PHP code snippet. What will it output?
<?php $value = "123abc"; $intValue = (int)$value; echo $intValue; ?>
When casting a string to int in PHP, it converts the initial numeric part.
PHP converts the string "123abc" to integer by reading the initial numeric characters until a non-digit is found. So it becomes 123.
What will this PHP code print?
<?php $floatVal = 3.99; $intVal = (int)$floatVal; echo $intVal; ?>
Casting float to int truncates the decimal part.
When casting a float to int in PHP, the decimal part is removed (not rounded). So 3.99 becomes 3.
What error will this PHP code produce?
<?php $array = [1, 2, 3]; $intVal = (int)$array; echo $intVal; ?>
Arrays cannot be cast to int in PHP.
PHP throws a fatal error when trying to cast an array to int because this conversion is not allowed.
What will this PHP code output?
<?php $boolVal = true; $strVal = (string)$boolVal; echo strlen($strVal); ?>
True converts to string "1" and false to empty string "".
Casting true to string results in "1" which has length 1.
What will this PHP code output?
<?php $value = "10 apples"; $floatVal = (float)$value; $intVal = (int)$floatVal; echo $intVal + 5; ?>
Casting string to float reads initial number, then casting float to int truncates decimals.
"10 apples" cast to float becomes 10.0, then to int becomes 10. Adding 5 results in 15.