Consider the following PHP code snippet:
$var = '123abc'; settype($var, 'int'); echo $var;
What will be printed?
$var = '123abc'; settype($var, 'int'); echo $var;
When converting a string to int, PHP reads the number from the start until it hits a non-digit.
PHP converts the string '123abc' to integer by reading the initial digits '123'. The rest is ignored, so the output is 123.
What will this PHP code print?
$value = 0; settype($value, 'bool'); echo $value ? 'true' : 'false';
$value = 0; settype($value, 'bool'); echo $value ? 'true' : 'false';
Remember that 0 is considered false in boolean context.
After settype to boolean, 0 becomes false, so the ternary prints 'false'.
Analyze this PHP code:
$arr = [1, 2, 3]; settype($arr, 'string'); echo $arr;
What will it output?
$arr = [1, 2, 3]; settype($arr, 'string'); echo $arr;
When converting an array to string, PHP returns a fixed word.
PHP converts an array to the string 'Array' when forced to string context.
Consider this PHP code:
$var = 5; settype($var, 'unknown');
What happens when this runs?
$var = 5; settype($var, 'unknown');
PHP warns if the type string is not recognized.
settype() issues a warning if the type is invalid but does not stop execution.
What is the count of elements in $data after this code runs?
$data = '1,2,3'; settype($data, 'array'); $count = count($data); echo $count;
$data = '1,2,3'; settype($data, 'array'); $count = count($data); echo $count;
Converting a string to array with settype wraps it inside an array.
settype converts the string into an array with one element: the original string. So count is 1.