0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.