Bird
Raised Fist0
PHPprogramming~20 mins

$_GET for URL parameters in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP $_GET Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using $_GET?
Consider the URL: http://example.com/page.php?name=Alice&age=30. What will this PHP code output?
PHP
<?php
$name = $_GET['name'];
$age = $_GET['age'];
echo "Name: $name, Age: $age";
?>
AName: , Age: 30
BName: Alice, Age: 30
CName: Alice, Age:
DName: , Age:
Attempts:
2 left
💡 Hint
Check how $_GET accesses URL parameters by their keys.
🧠 Conceptual
intermediate
2:00remaining
What happens if a URL parameter is missing in $_GET?
Given the URL http://example.com/page.php?user=Bob, what will be the output of this code?
PHP
<?php
$user = $_GET['user'];
$age = $_GET['age'];
echo "User: $user, Age: $age";
?>
AUser: Bob, Age: 0
BUser: Bob, Age: NULL
CUser: Bob, Age:
DUser: Bob, Age: Notice: Undefined index: age
Attempts:
2 left
💡 Hint
Accessing a missing key in $_GET causes a notice error.
🔧 Debug
advanced
2:00remaining
Why does this PHP code not output the expected $_GET value?
URL: http://example.com/page.php?city=Paris Code:
<?php
$city = $_GET[city];
echo "City: $city";
?>
What is the problem?
AMissing quotes around 'city' key causes a PHP notice or error.
BThe $_GET array is empty because URL parameters are not passed.
CThe variable $city is overwritten before echo.
DThe echo statement syntax is incorrect.
Attempts:
2 left
💡 Hint
Array keys must be quoted strings in PHP.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP code with multiple $_GET parameters?
URL: http://example.com/page.php?item=book&price=15&discount=5 Code:
<?php
$item = $_GET['item'];
$price = $_GET['price'];
$discount = $_GET['discount'];
$total = $price - $discount;
echo "Item: $item, Total Price: $total";
?>
AItem: book, Total Price: 20
BItem: book, Total Price: 15-5
CItem: book, Total Price: 10
DItem: book, Total Price: 0
Attempts:
2 left
💡 Hint
URL parameters are strings; PHP converts them to numbers in arithmetic.
📝 Syntax
expert
2:00remaining
Which option causes a syntax error when accessing $_GET parameters?
Identify the code snippet that will cause a PHP syntax error.
A$value = $_GET['key']
B$value = $_GET[key];
C$value = $_GET["key"];
D$value = $_GET['key'];
Attempts:
2 left
💡 Hint
Check for missing semicolons in PHP statements.

Practice

(1/5)
1. What does the PHP $_GET superglobal do?
easy
A. It retrieves data sent in the URL query string after the question mark.
B. It stores data sent via a form using the POST method.
C. It saves session data for the user.
D. It connects to a database.

Solution

  1. Step 1: Understand the purpose of $_GET

    $_GET is used to get data sent through the URL query string, which comes after the question mark.
  2. Step 2: Compare with other PHP superglobals

    $_POST is for form data sent via POST, sessions are stored in $_SESSION, and database connections use other functions.
  3. Final Answer:

    It retrieves data sent in the URL query string after the question mark. -> Option A
  4. Quick Check:

    $_GET reads URL parameters [OK]
Hint: Remember: $_GET reads URL after '?' [OK]
Common Mistakes:
  • Confusing $_GET with $_POST
  • Thinking $_GET stores session data
  • Assuming $_GET connects to databases
2. Which of the following is the correct way to access a URL parameter named id using $_GET in PHP?
easy
A. $_GET->id
B. $GET['id']
C. get['id']
D. $_GET['id']

Solution

  1. Step 1: Recall PHP array syntax for superglobals

    $_GET is an associative array, so parameters are accessed with square brackets and quotes.
  2. Step 2: Check each option

    $_GET['id'] uses correct syntax: $_GET['id']. Others use wrong syntax or variable names.
  3. Final Answer:

    $_GET['id'] -> Option D
  4. Quick Check:

    Use square brackets and quotes for $_GET keys [OK]
Hint: Use $_GET['param'] to get URL parameters [OK]
Common Mistakes:
  • Using arrow (->) instead of brackets
  • Missing the dollar sign ($)
  • Using wrong variable names like $GET
3. What will the following PHP code output if the URL is page.php?name=Alice?
<?php
echo 'Hello, ' . $_GET['name'];
?>
medium
A. Hello, Alice
B. Hello,
C. Notice: Undefined index: name
D. Syntax Error

Solution

  1. Step 1: Identify the URL parameter

    The URL has name=Alice, so $_GET['name'] will be 'Alice'.
  2. Step 2: Understand the echo statement

    The code concatenates 'Hello, ' with the value of $_GET['name'], so it prints 'Hello, Alice'.
  3. Final Answer:

    Hello, Alice -> Option A
  4. Quick Check:

    URL param 'name' = 'Alice' outputs greeting [OK]
Hint: Echo $_GET['param'] prints URL parameter value [OK]
Common Mistakes:
  • Assuming empty output if parameter exists
  • Confusing undefined index error when parameter is present
  • Thinking code causes syntax error
4. Consider this PHP code snippet:
<?php
echo 'User ID: ' . $_GET['user_id'];
?>
What is the main problem if the URL does not include user_id parameter?
medium
A. It will print 'User ID: ' with no value.
B. It will cause a Notice about undefined index.
C. It will cause a fatal error and stop the script.
D. It will automatically assign a default value.

Solution

  1. Step 1: Understand accessing missing $_GET keys

    Accessing $_GET['user_id'] when it is not set triggers a PHP Notice about undefined index.
  2. Step 2: Differentiate error types

    This is not a fatal error, so the script continues, but a warning appears. No default value is assigned automatically.
  3. Final Answer:

    It will cause a Notice about undefined index. -> Option B
  4. Quick Check:

    Missing $_GET key triggers Notice [OK]
Hint: Check if parameter exists before using $_GET['key'] [OK]
Common Mistakes:
  • Assuming no error if parameter missing
  • Thinking it causes fatal error
  • Expecting automatic default values
5. You want to safely get a URL parameter page and show it, but if it is missing, show 'home' instead. Which PHP code correctly does this?
hard
A. echo $_GET['page'] ?? 'home';
B. echo $_GET['page'] ? 'home' : $_GET['page'];
C. echo isset($_GET['page']) ? $_GET['page'] : 'home';
D. echo $_GET['page'] || 'home';

Solution

  1. Step 1: Understand safe $_GET access

    To safely access URL parameters without triggering 'undefined index' notices, use isset() to check if the key exists.
  2. Step 2: Check each option

    echo isset($_GET['page']) ? $_GET['page'] : 'home';: echo isset($_GET['page']) ? $_GET['page'] : 'home'; checks existence first, avoiding notices, and provides 'home' default if missing. echo $_GET['page'] ?? 'home'; (??) does NOT trigger a notice in PHP 7+, so this explanation is incorrect. However, echo $_GET['page'] ?? 'home'; is valid syntax and safe. echo $_GET['page'] ? 'home' : $_GET['page']; inverts ternary logic. D uses ||, triggers notice, and mishandles empty strings.
  3. Final Answer:

    echo isset($_GET['page']) ? $_GET['page'] : 'home'; -> Option C
  4. Quick Check:

    isset ternary avoids notice for missing $_GET keys [OK]
Hint: Use isset($_GET['param']) ? $_GET['param'] : 'default' [OK]
Common Mistakes:
  • Using ?? which still triggers undefined index notice
  • Wrong ternary order
  • Using || which triggers notice and handles empty strings wrong
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard