0
0
PHPprogramming~20 mins

PHP process model per request - Practice Problems & Coding Challenges

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