Bird
Raised Fist0
PHPprogramming~20 mins

Comparison with long-running servers (Node.js) in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Long-running Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:00remaining
PHP script execution time behavior

What will be the output of this PHP script when run from the command line?

<?php
sleep(2);
echo "Hello World";
?>
PHP
<?php
sleep(2);
echo "Hello World";
?>
APrints 'Hello World' after 2 seconds delay
BThrows a runtime error due to sleep
CPrints nothing and exits immediately
DPrints 'Hello World' immediately
Attempts:
2 left
💡 Hint

Think about how PHP scripts run and what sleep() does.

🧠 Conceptual
intermediate
1:30remaining
Difference in request handling between PHP and Node.js

Which statement best describes how PHP and Node.js handle incoming web requests?

APHP uses a single long-running process; Node.js creates a new process per request
BBoth PHP and Node.js use a single process that blocks on each request
CBoth PHP and Node.js create a new process for each request
DPHP runs a new process for each request; Node.js uses a single long-running process to handle many requests asynchronously
Attempts:
2 left
💡 Hint

Think about how PHP scripts are executed by web servers versus how Node.js servers work.

❓ Predict Output
advanced
2:00remaining
Memory usage difference in PHP vs Node.js long-running server

Consider this PHP script and this Node.js server snippet. Which statement about their memory usage is true after handling many requests?

PHP script (simplified):
<?php
// runs once per request
$data = range(1, 100000);
echo count($data);
?>
Node.js server snippet:
const http = require('http');
let data = [];
http.createServer((req, res) => {
  data = Array.from({length: 100000}, (_, i) => i + 1);
  res.end(data.length.toString());
}).listen(3000);
ABoth PHP and Node.js free memory after each request automatically
BPHP frees memory after each request; Node.js keeps growing memory usage with each request
CNode.js frees memory after each request; PHP keeps memory until server restart
DBoth PHP and Node.js keep memory usage constant regardless of requests
Attempts:
2 left
💡 Hint

Think about how long the data array lives in each environment.

🔧 Debug
advanced
1:30remaining
Identify the error in this Node.js server code

What error will this Node.js code produce when run?

const http = require('http');
http.createServer((req, res) => {
  res.write('Hello');
  res.end();
  res.write('World');
}).listen(3000);
PHP
const http = require('http');
http.createServer((req, res) => {
  res.write('Hello');
  res.end();
  res.write('World');
}).listen(3000);
AThrows an error because res.write is called after res.end
BPrints 'HelloWorld' correctly
CServer crashes immediately on start
DNo output is sent to client
Attempts:
2 left
💡 Hint

Consider what happens if you write to the response after ending it.

🧠 Conceptual
expert
2:00remaining
Why might Node.js be preferred for real-time applications over PHP?

Which reason best explains why Node.js is often chosen for real-time apps compared to PHP?

ANode.js restarts the server process for each request, improving real-time performance
BPHP has built-in support for WebSockets and real-time events, unlike Node.js
CNode.js uses a single-threaded event loop allowing non-blocking I/O, enabling many simultaneous connections efficiently
DPHP runs continuously in memory, making it faster for real-time apps
Attempts:
2 left
💡 Hint

Think about how Node.js handles input/output operations compared to PHP.

Practice

(1/5)
1. Which statement best describes how PHP handles variables compared to Node.js in a web server context?
easy
A. PHP keeps variables in memory between requests, but Node.js resets them every time.
B. Both PHP and Node.js reset variables on every request.
C. PHP resets variables on every request, while Node.js keeps variables in memory between requests.
D. Both PHP and Node.js keep variables in memory between requests.

Solution

  1. Step 1: Understand PHP's request model

    PHP scripts start fresh on each request, so all variables are reset.
  2. Step 2: Understand Node.js server behavior

    Node.js servers run continuously, so variables can persist in memory across requests.
  3. Final Answer:

    PHP resets variables on every request, while Node.js keeps variables in memory between requests. -> Option C
  4. Quick Check:

    PHP resets, Node.js persists [OK]
Hint: Remember PHP restarts each request; Node.js runs continuously [OK]
Common Mistakes:
  • Thinking PHP keeps variables in memory between requests
  • Assuming Node.js resets variables like PHP
  • Confusing session storage with variable persistence
2. Which of the following PHP code snippets correctly starts a session to preserve data across requests?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Identify the correct PHP function to start a session

    The correct function is session_start() to begin or resume a session.
  2. Step 2: Check the code syntax

    uses session_start() correctly; others use invalid function names.
  3. Final Answer:

    <?php session_start(); $_SESSION['count'] = 1; ?> -> Option A
  4. Quick Check:

    Use session_start() to begin sessions [OK]
Hint: Use session_start() exactly to enable sessions [OK]
Common Mistakes:
  • Using incorrect function names like start_session()
  • Forgetting to call session_start() before using $_SESSION
  • Assuming sessions start automatically
3. Consider this PHP code snippet run on two separate requests:
<?php
session_start();
if (!isset($_SESSION['visits'])) {
    $_SESSION['visits'] = 1;
} else {
    $_SESSION['visits']++;
}
echo $_SESSION['visits'];
?>
What will be the output on the second request?
medium
A. 1
B. 2
C. 0
D. Error

Solution

  1. Step 1: Analyze session variable initialization

    On the first request, $_SESSION['visits'] is not set, so it is initialized to 1.
  2. Step 2: Analyze second request behavior

    On the second request, $_SESSION['visits'] exists, so it increments by 1, becoming 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Session increments visits count correctly [OK]
Hint: Sessions keep data; visits count increases each request [OK]
Common Mistakes:
  • Assuming visits resets to 1 every request
  • Forgetting to call session_start()
  • Expecting an error due to undefined index
4. This PHP script is intended to count visits using sessions, but it always outputs 1 on every request:
<?php
if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 1;
} else {
    $_SESSION['count']++;
}
echo $_SESSION['count'];
?>
What is the most likely cause of this problem?
medium
A. The session_start() function is missing, so sessions don't work.
B. The $_SESSION variable is overwritten elsewhere in the code.
C. The $_SESSION['count'] variable is reset manually each time.
D. The echo statement is inside the else block only.

Solution

  1. Step 1: Check for session initialization

    The code does not call session_start(), so PHP does not load or save session data.
  2. Step 2: Understand session behavior without session_start()

    Without starting the session, $_SESSION is empty each request, so count resets to 1 every time.
  3. Final Answer:

    The session_start() function is missing, so sessions don't work. -> Option A
  4. Quick Check:

    Always call session_start() before using $_SESSION [OK]
Hint: Always start session with session_start() to keep data [OK]
Common Mistakes:
  • Assuming $_SESSION works without session_start()
  • Thinking variable reset is due to manual code
  • Ignoring session initialization step
5. You want to simulate a long-running server behavior in PHP to keep a counter in memory across multiple requests without using sessions or databases. Which approach is the best to achieve this?
hard
A. Restart the PHP server after each request to reset variables.
B. Store the counter in a local variable inside a PHP script executed per request.
C. Use cookies to store the counter on the client side only.
D. Use a PHP daemon script running continuously and communicate via sockets or shared memory.

Solution

  1. Step 1: Understand PHP's request lifecycle

    PHP scripts run fresh on each request, so local variables reset automatically.
  2. Step 2: Identify how to keep data in memory across requests

    To keep data in memory like Node.js, PHP must run as a long-running process (daemon) and communicate externally.
  3. Step 3: Evaluate options

    Use a PHP daemon script running continuously and communicate via sockets or shared memory. describes running a PHP daemon with sockets/shared memory, which simulates long-running server behavior.
  4. Final Answer:

    Use a PHP daemon script running continuously and communicate via sockets or shared memory. -> Option D
  5. Quick Check:

    Long-running PHP needs daemon + IPC, not per-request scripts [OK]
Hint: Run PHP as daemon with IPC to keep data in memory [OK]
Common Mistakes:
  • Thinking local variables persist across requests
  • Confusing cookies with server memory
  • Restarting server resets all 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