Consider the following PHP code snippet. What will it output?
<?php $a = PHP_INT_MAX; $b = $a + 1; echo gettype($b); ?>
Think about what happens when you add 1 to the largest integer PHP can hold.
Adding 1 to PHP_INT_MAX causes integer overflow, so PHP converts the value to a float (double).
What will be the value of $result after executing this PHP code?
<?php $result = (int) "123abc" + 10; echo $result; ?>
How does PHP convert a string to an integer when the string starts with digits?
PHP converts the initial numeric part of the string to integer, ignoring the rest. So "123abc" becomes 123, plus 10 equals 133.
What error or output will this PHP code produce?
<?php $val = 1 << 65; echo $val; ?>
Consider how PHP handles bit shifts larger than the integer size on 64-bit systems.
In PHP, shifting an integer by more than or equal to the number of bits results in 0. So 1 << 65 outputs 0.
Which of the following PHP code snippets will cause a syntax error?
Check how PHP handles numeric literals and commas.
Option B uses a comma inside a number literal, which is invalid syntax in PHP.
Given the following PHP code, how many items does the array $arr contain?
<?php $arr = []; $arr[1] = 'a'; $arr[1.5] = 'b'; $arr[true] = 'c'; $arr[null] = 'd'; $arr['1'] = 'e'; ?>
Remember how PHP converts array keys to integers.
PHP converts keys to integers or strings. Here, keys 1, 1.5, true, and '1' all become integer 1, overwriting each other. Null becomes empty string key. So keys are 1 and "" resulting in 2 items.