Bird
0
0

Which of the following is the correct way to add a string number and an integer in PHP?

easy📝 Syntax Q3 of 15
PHP - Type Handling
Which of the following is the correct way to add a string number and an integer in PHP?
A$result = '10' * 'five';
B$result = '10' . 5;
C$result = '10' + 5;
D$result = '10' - '5';
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct arithmetic addition syntax

    Using '+' operator adds numeric values after coercion.
  2. Step 2: Check each option

    A multiplies '10' * 'five' (10 * 0 = 0). B concatenates to '105'. C subtracts '10' - '5' = 5. D adds '10' + 5 = 15. Only D correctly adds.
  3. Final Answer:

    $result = '10' + 5; -> Option C
  4. Quick Check:

    Use '+' for numeric addition with strings [OK]
Quick Trick: Use '+' to add numbers, '.' to join strings [OK]
Common Mistakes:
  • Using '.' for addition
  • Subtracting instead of adding
  • Multiplying with non-numeric string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes