Bird
0
0

In PHP, what happens when you add a string containing a number to an integer, like '5' + 3?

easy📝 Conceptual Q11 of 15
PHP - Type Handling
In PHP, what happens when you add a string containing a number to an integer, like '5' + 3?
APHP throws a type error because you can't add string and integer.
BThe result is the string '53' by concatenating '5' and '3'.
CThe string '5' is converted to the number 5, then added to 3, resulting in 8.
DThe operation returns null because of incompatible types.
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP type coercion in addition

    PHP automatically converts strings containing numbers to integers or floats when used in arithmetic operations.
  2. Step 2: Apply addition after conversion

    The string '5' becomes the integer 5, then 5 + 3 equals 8.
  3. Final Answer:

    8 -> Option C
  4. Quick Check:

    '5' + 3 = 8 [OK]
Quick Trick: Strings with numbers convert to numbers in math operations [OK]
Common Mistakes:
  • Thinking PHP concatenates strings with + operator
  • Expecting a type error on mixed types
  • Assuming the result stays a string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes