Bird
Raised Fist0
PHPprogramming~20 mins

PHP process model per request - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP Process Model 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 script?

Consider this PHP script executed on a web server for a single HTTP request:

<?php
$counter = 0;
function increment() {
    global $counter;
    $counter++;
    echo $counter . "\n";
}
increment();
increment();
?>

What will be printed when this script runs?

PHP
<?php
$counter = 0;
function increment() {
    global $counter;
    $counter++;
    echo $counter . "\n";
}
increment();
increment();
?>
A
1
2
B
2
4
C
1
1
D
0
1
Attempts:
2 left
💡 Hint

Remember that PHP variables do not keep their values between requests unless stored externally.

🧠 Conceptual
intermediate
1:30remaining
How does PHP handle variables between HTTP requests?

Which statement best describes how PHP manages variables across multiple HTTP requests?

APHP keeps all variables in memory between requests for faster access.
BPHP automatically saves variables to a file after each request and reloads them on the next.
CPHP resets all variables at the start of each request; variables do not persist unless stored externally.
DPHP shares variables between requests using global shared memory by default.
Attempts:
2 left
💡 Hint

Think about what happens when you refresh a PHP page multiple times.

🔧 Debug
advanced
2:30remaining
Why does this PHP script always print 1 on every request?

Look at this PHP code snippet:

<?php
session_start();
if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 1;
} else {
    $_SESSION['count'] = 1;
}
echo $_SESSION['count'];
?>

Why does it always print 1 even after refreshing the page multiple times?

ABecause the session is not started properly, so <code>$_SESSION</code> is empty every time.
BBecause the code resets <code>$_SESSION['count']</code> to 1 in both the if and else blocks.
CBecause the browser does not support cookies, so session ID is lost.
DBecause PHP sessions do not persist data between requests.
Attempts:
2 left
💡 Hint

Check what happens inside the if and else blocks.

📝 Syntax
advanced
2:00remaining
Which PHP code snippet correctly demonstrates per-request initialization?

Which of the following PHP code snippets correctly initializes a variable fresh for each HTTP request and then prints it?

A
&lt;?php
$count++;
echo $count;
?&gt;
B
&lt;?php
static $count = 0;
$count++;
echo $count;
?&gt;
C
&lt;?php
global $count;
$count++;
echo $count;
?&gt;
D
&lt;?php
$count = 0;
$count++;
echo $count;
?&gt;
Attempts:
2 left
💡 Hint

Remember that static variables keep their value during the script execution but are reset on new requests.

🚀 Application
expert
3:00remaining
How to persist data across PHP requests without sessions?

You want to keep a counter that increases every time a PHP page is loaded, but you cannot use sessions. Which approach below will correctly persist the counter across requests?

AStore the counter in a text file and read/write it on each request.
BUse a static variable inside the PHP script to keep the counter value.
CDeclare the counter as a global variable at the top of the script.
DUse a local variable inside a function and increment it.
Attempts:
2 left
💡 Hint

Think about where data can be saved outside the PHP process.

Practice

(1/5)
1. What happens to PHP variables after a web request finishes?
easy
A. They become global variables accessible by all users
B. They keep their values for the next request automatically
C. They are stored permanently in the server memory
D. They are reset and lost unless saved externally

Solution

  1. Step 1: Understand PHP request lifecycle

    PHP starts fresh for each request, so variables exist only during that request.
  2. Step 2: Identify variable persistence

    Variables reset after request ends unless saved in sessions or databases.
  3. Final Answer:

    They are reset and lost unless saved externally -> Option D
  4. Quick Check:

    PHP variables reset each request = D [OK]
Hint: Remember: PHP starts fresh every request, no variable memory [OK]
Common Mistakes:
  • Thinking variables persist automatically between requests
  • Confusing PHP with long-running server processes
  • Assuming global variables keep values across users
2. Which PHP code snippet correctly shows the start of a new request process?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Analyze variable initialization

    In a new request, variables start fresh; $count = 0 sets it properly.
  2. Step 2: Check other options for persistence

    Options A, B, D try to increment without initialization or rely on static/session which need extra setup.
  3. Final Answer:

    <?php $count = 0; echo $count; ?> -> Option C
  4. Quick Check:

    New request means fresh variables = C [OK]
Hint: Fresh request means variables start from scratch [OK]
Common Mistakes:
  • Assuming static variables keep value across requests
  • Using session without starting it
  • Incrementing uninitialized variables
3. What will be the output of this PHP code when run twice in separate requests?
<?php
session_start();
if (!isset($_SESSION['visits'])) {
  $_SESSION['visits'] = 1;
} else {
  $_SESSION['visits']++;
}
echo $_SESSION['visits'];
?>
medium
A. 1 then 2
B. 1 then 1
C. 2 then 3
D. Error on second request

Solution

  1. Step 1: Understand session persistence

    Session data keeps values across requests, so visits count increases.
  2. Step 2: Trace two requests

    First request sets visits=1; second request increments to 2.
  3. Final Answer:

    1 then 2 -> Option A
  4. Quick Check:

    Session stores visits count = 1 then 2 [OK]
Hint: Sessions keep data between requests, variables do not [OK]
Common Mistakes:
  • Thinking variables persist without sessions
  • Forgetting to call session_start()
  • Expecting static variables to persist across requests
4. Identify the error in this PHP code snippet that tries to keep a counter across requests:
<?php
$count = 0;
$count++;
echo $count;
?>
medium
A. Variables reset each request, so counter never increases
B. Syntax error due to missing semicolon
C. Session not started, so counter is undefined
D. Static variable needed to keep count

Solution

  1. Step 1: Check variable behavior per request

    $count is set to 0 every time, so increment always starts fresh.
  2. Step 2: Identify persistence issue

    Without sessions or external storage, counter resets each request.
  3. Final Answer:

    Variables reset each request, so counter never increases -> Option A
  4. Quick Check:

    Variables reset each request = A [OK]
Hint: Variables reset each request; use sessions to persist [OK]
Common Mistakes:
  • Assuming static variables persist across requests
  • Confusing syntax errors with logic errors
  • Forgetting to start sessions for persistence
5. You want to count unique visitors to your PHP website. Which approach correctly uses PHP's process model per request to achieve this?
hard
A. Use a static variable in PHP script to count visitors
B. Store visitor count in a database and increment on each request
C. Increment a PHP variable without sessions or storage
D. Rely on PHP variables to keep count across requests

Solution

  1. Step 1: Understand PHP process model limits

    PHP variables reset each request, so they can't keep visitor count.
  2. Step 2: Identify persistent storage method

    Database stores data permanently and can be updated each request.
  3. Final Answer:

    Store visitor count in a database and increment on each request -> Option B
  4. Quick Check:

    Persistent storage needed for visitor count = A [OK]
Hint: Use database or sessions to save data beyond request [OK]
Common Mistakes:
  • Using static or normal variables expecting persistence
  • Ignoring need for external storage
  • Confusing sessions with static variables
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