0
0
PHPprogramming~20 mins

Script execution and memory reset in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Script Execution Mastery
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 the following PHP code that uses a static variable inside a function. What will be printed after running the script?

PHP
<?php
function counter() {
    static $count = 0;
    $count++;
    echo $count . ' ';
}
counter();
counter();
counter();
?>
A1 1 1
B0 1 2
C1 2 3
D3 3 3
Attempts:
2 left
💡 Hint

Static variables keep their value between function calls.

Predict Output
intermediate
1:30remaining
What happens to variables after script execution ends?

In PHP, what happens to all variables after the script finishes running?

AAll variables are cleared from memory and reset for the next script run.
BVariables keep their values and are available in the next script execution automatically.
CVariables are saved to a file automatically for persistence.
DVariables are converted to global constants after script ends.
Attempts:
2 left
💡 Hint

Think about how PHP runs scripts for each request.

🔧 Debug
advanced
2:30remaining
Why does this PHP script output '1 1 1' instead of '1 2 3'?

Look at this PHP code snippet. It is intended to print 1 2 3 but prints 1 1 1 instead. Why?

PHP
<?php
function counter() {
    $count = 0;
    $count++;
    echo $count . ' ';
}
counter();
counter();
counter();
?>
ABecause $count is a local variable reset to 0 on every call.
BBecause $count is declared static but not incremented properly.
CBecause echo is buffering output incorrectly.
DBecause the function is called only once.
Attempts:
2 left
💡 Hint

Check how the variable $count is declared inside the function.

Predict Output
advanced
2:00remaining
What is the output of this PHP script using session variables?

This PHP script uses a session variable to count visits. What will it output on the third page load?

PHP
<?php
session_start();
if (!isset($_SESSION['visits'])) {
    $_SESSION['visits'] = 0;
}
$_SESSION['visits']++;
echo $_SESSION['visits'];
?>
AError: Undefined index 'visits'
B1
C0
D3
Attempts:
2 left
💡 Hint

Session variables keep data between page loads for the same user.

🧠 Conceptual
expert
3:00remaining
How does PHP handle memory between separate script executions?

Which statement best describes how PHP manages memory and variables between separate script executions in a typical web server environment?

APHP keeps all variables in memory between executions to speed up processing automatically.
BEach script execution starts with a fresh memory state; variables do not persist unless explicitly stored externally.
CVariables are saved in a shared memory pool accessible by all scripts without session or database use.
DPHP scripts share global variables across all users and requests by default.
Attempts:
2 left
💡 Hint

Think about how PHP runs scripts on a web server for each request.