Challenge - 5 Problems
PHP Persistence Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code?
Consider this PHP script run twice in separate requests. What will be the output on the second request?
PHP
<?php $count = 0; $count++; echo $count; ?>
Attempts:
2 left
💡 Hint
Variables in PHP do not keep their values between separate requests unless stored externally.
✗ Incorrect
Each time a PHP script runs, it starts fresh. The variable $count is set to 0 every time, so incrementing it results in 1 each request.
🧠 Conceptual
intermediate2:00remaining
Why do PHP variables not keep their values between requests?
Choose the best explanation for why PHP variables do not persist their values between different HTTP requests.
Attempts:
2 left
💡 Hint
Think about how PHP handles each request independently.
✗ Incorrect
PHP runs the script from start to end on each request. Variables are created and destroyed during that run, so they don't keep values between requests.
🔧 Debug
advanced2:00remaining
Why does this PHP code not remember the count between requests?
This PHP code tries to count how many times the page was loaded, but it always shows 1. Why?
PHP
<?php $count = 0; $count++; echo "Page loaded $count times."; ?>
Attempts:
2 left
💡 Hint
Think about what happens to variables after the script finishes.
✗ Incorrect
The variable $count is set to 0 every time the script runs, so incrementing it only makes it 1 each time. To remember count, you need to store it outside the script.
📝 Syntax
advanced2:00remaining
Which PHP code snippet correctly uses a session to persist a variable between requests?
Select the code that will keep the count variable between page loads using PHP sessions.
Attempts:
2 left
💡 Hint
Sessions require initialization and checking if the variable exists first.
✗ Incorrect
Option C correctly starts the session, initializes the count if not set, increments it, and echoes it. Other options either don't initialize or use wrong variables.
🚀 Application
expert3:00remaining
How to persist user data across multiple PHP requests?
You want to remember a user's name across multiple page visits without asking again. Which method is the best to persist this data?
Attempts:
2 left
💡 Hint
Think about how PHP handles variables and what can keep data between requests.
✗ Incorrect
PHP variables only last during one script run. Sessions allow data to be saved on the server and accessed across multiple requests for the same user.