0
0
PHPprogramming~20 mins

Why variables do not persist between requests in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Persistence Pro
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?
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;
?>
AError: Undefined variable
B1
C0
D2
Attempts:
2 left
💡 Hint
Variables in PHP do not keep their values between separate requests unless stored externally.
🧠 Conceptual
intermediate
2: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.
ABecause PHP scripts run fresh on each request and variables exist only during script execution.
BBecause PHP variables are stored in the browser and cleared on refresh.
CBecause PHP automatically deletes variables after 5 seconds.
DBecause PHP variables are saved only if the server is restarted.
Attempts:
2 left
💡 Hint
Think about how PHP handles each request independently.
🔧 Debug
advanced
2: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.";
?>
ABecause $count is reset to 0 on every request, so it never accumulates.
BBecause PHP does not support variables named $count.
CBecause echo does not display variables correctly.
DBecause the server caches the output and does not run the script again.
Attempts:
2 left
💡 Hint
Think about what happens to variables after the script finishes.
📝 Syntax
advanced
2: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.
A
&lt;?php
session_start();
$count++;
echo $count;
?&gt;
B
&lt;?php
$count = 0;
$count++;
echo $count;
?&gt;
C
&lt;?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
}
$_SESSION['count']++;
echo $_SESSION['count'];
?&gt;
D
&lt;?php
session_start();
$_SESSION['count']++;
echo $_SESSION['count'];
?&gt;
Attempts:
2 left
💡 Hint
Sessions require initialization and checking if the variable exists first.
🚀 Application
expert
3: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?
AStore the name in a variable and expect it to persist after the script ends.
BStore the name in a local PHP variable and rely on the script to keep it.
CStore the name in a variable inside a function without returning it.
DStore the name in a PHP session variable and start the session on each page.
Attempts:
2 left
💡 Hint
Think about how PHP handles variables and what can keep data between requests.