Bird
0
0

You want to create a PHP script that reads two URL parameters, a and b, adds them as integers, and prints the result. Which code snippet correctly does this?

hard📝 Application Q8 of 15
PHP - Superglobals and Web Context
You want to create a PHP script that reads two URL parameters, a and b, adds them as integers, and prints the result. Which code snippet correctly does this?
A<?php $a = $_GET['a']; $b = $_GET['b']; echo $a . $b; ?>
B<?php $a = $_GET['a']; $b = $_GET['b']; echo $a - $b; ?>
C<?php $a = $_POST['a']; $b = $_POST['b']; echo $a + $b; ?>
D<?php $a = (int)$_GET['a']; $b = (int)$_GET['b']; echo $a + $b; ?>
Step-by-Step Solution
Solution:
  1. Step 1: Convert URL parameters to integers

    Use (int) to cast $_GET['a'] and $_GET['b'] to integers to ensure numeric addition.
  2. Step 2: Add and echo the sum

    Adding $a and $b after casting prints the correct sum.
  3. Final Answer:

    Code casting $_GET params to int and adding -> Option D
  4. Quick Check:

    Cast to int before adding URL params [OK]
Quick Trick: Cast $_GET values to int before math operations [OK]
Common Mistakes:
  • Concatenating strings instead of adding
  • Using $_POST instead of $_GET
  • Subtracting instead of adding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes