What will be the output of this PHP script when run from the command line?
<?php sleep(2); echo "Hello World"; ?>
<?php sleep(2); echo "Hello World"; ?>
Think about how PHP scripts run and what sleep() does.
PHP scripts run from start to finish each time they are called. The sleep(2) pauses execution for 2 seconds, so the output appears after the delay.
Which statement best describes how PHP and Node.js handle incoming web requests?
Think about how PHP scripts are executed by web servers versus how Node.js servers work.
PHP typically runs a new process or thread for each request, starting fresh each time. Node.js runs a single long-running process that handles many requests asynchronously without restarting.
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);Think about how long the data array lives in each environment.
PHP runs fresh each request, so memory is freed after the script ends. Node.js runs a long server process, so if data is reassigned but not cleared properly, memory usage can grow over time.
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);const http = require('http'); http.createServer((req, res) => { res.write('Hello'); res.end(); res.write('World'); }).listen(3000);
Consider what happens if you write to the response after ending it.
Calling res.write after res.end causes an error because the response is already finished and cannot be written to.
Which reason best explains why Node.js is often chosen for real-time apps compared to PHP?
Think about how Node.js handles input/output operations compared to PHP.
Node.js uses an event-driven, non-blocking I/O model with a single-threaded event loop, which makes it efficient for handling many real-time connections simultaneously. PHP typically runs a new process per request, which is less efficient for real-time.