Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Type Handling
What will be the output of the following PHP code?
$var = '15.7abc';
settype($var, 'float');
echo $var;
A15.7abc
B15.7
C0
D15
Step-by-Step Solution
Solution:
  1. Step 1: Understand settype with 'float'

    The string '15.7abc' is converted to a float. PHP reads the initial numeric part '15.7' and ignores the rest.
  2. Step 2: Output the converted value

    After conversion, $var holds the float 15.7, so echo outputs '15.7'.
  3. Final Answer:

    15.7 -> Option B
  4. Quick Check:

    settype float parses leading number only [OK]
Quick Trick: settype float extracts leading numeric part only [OK]
Common Mistakes:
  • Expecting full string output
  • Assuming conversion to integer
  • Thinking it outputs zero for invalid parts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes