Bird
Raised Fist0
PHPprogramming~20 mins

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

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

Practice

(1/5)
1. What happens to variables in a PHP script after the script finishes running?
easy
A. Variables are saved permanently in the script file
B. Variables keep their values until the server restarts
C. All variables and memory are reset and cleared
D. Variables are shared with other scripts automatically

Solution

  1. Step 1: Understand PHP script execution

    PHP runs scripts from top to bottom each time the page loads.
  2. Step 2: Memory and variable reset after execution

    After the script finishes, all variables and memory used are cleared to keep the environment clean.
  3. Final Answer:

    All variables and memory are reset and cleared -> Option C
  4. Quick Check:

    PHP resets variables after script ends = C [OK]
Hint: Remember PHP resets memory after each script run [OK]
Common Mistakes:
  • Thinking variables keep values between page loads
  • Assuming variables save to the script file
  • Believing variables are shared across scripts automatically
2. Which of the following is the correct way to start a PHP script that resets variables each time it runs?
easy
A.
B.
C.
D. // PHP code here

Solution

  1. Step 1: Identify correct PHP tags

    PHP scripts start with to run properly.
  2. Step 2: Understand script resets

    Each time the script runs, variables reset automatically without extra commands.
  3. Final Answer:

    -> Option A
  4. Quick Check:

    Correct PHP tags = A [OK]
Hint: Use tags to start PHP scripts [OK]
Common Mistakes:
  • Using <script> tags which are for JavaScript
  • Trying to call reset() function which doesn't reset variables
  • Using incorrect <php> tags
3. Consider this PHP code run twice in a row (two separate page loads):
<?php
$counter = 0;
$counter++;
echo $counter;
?>

What will be the output each time the script runs?
medium
A. 1 then 1
B. 0 then 1
C. 1 then 2
D. 0 then 0

Solution

  1. Step 1: Analyze variable initialization

    Each time the script runs, $counter is set to 0 at the start.
  2. Step 2: Increment and output

    $counter++ increases $counter to 1, then echo prints 1 each time.
  3. Final Answer:

    1 then 1 -> Option A
  4. Quick Check:

    Variables reset each run, so output is 1 each time [OK]
Hint: Variables reset each run, so counter restarts at 0 [OK]
Common Mistakes:
  • Assuming $counter keeps incrementing across runs
  • Confusing pre-increment and post-increment
  • Thinking echo prints 0 first
4. You wrote this PHP code:
<?php
$sum = 10;
$sum += 5;
?>

But after refreshing the page, $sum is always 15. You want $sum to keep increasing by 5 each time the page loads. What is the problem?
medium
A. You need to use a loop to increase $sum
B. The += operator does not add values correctly
C. You forgot to declare $sum as global
D. PHP resets variables after each script run, so $sum starts at 10 every time

Solution

  1. Step 1: Understand variable reset behavior

    PHP clears all variables after each script finishes, so $sum resets to 10 every time.
  2. Step 2: Why $sum does not keep increasing

    Because $sum is reinitialized to 10 on each page load, adding 5 results in 15 always.
  3. Final Answer:

    PHP resets variables after each script run, so $sum starts at 10 every time -> Option D
  4. Quick Check:

    Variables reset each run = D [OK]
Hint: Variables reset each run; use sessions or files to keep values [OK]
Common Mistakes:
  • Thinking += operator is faulty
  • Assuming global declaration keeps value between runs
  • Believing loops affect variable persistence
5. You want to count how many times a PHP page has been loaded by visitors. Since PHP resets variables each run, which method should you use to keep the count across page loads?
hard
A. Declare the count variable as global inside the script
B. Store the count in a session or a file/database to persist data
C. Use a loop to increment the count variable inside the script
D. Reset the count variable to zero at the start of the script

Solution

  1. Step 1: Recognize PHP variable reset limitation

    PHP variables reset after each script run, so count cannot persist in normal variables.
  2. Step 2: Use persistent storage

    Sessions, files, or databases store data between page loads to keep count.
  3. Final Answer:

    Store the count in a session or a file/database to persist data -> Option B
  4. Quick Check:

    Persistent storage needed for count across runs = B [OK]
Hint: Use sessions or files to save data between page loads [OK]
Common Mistakes:
  • Thinking global variables keep values between runs
  • Trying to use loops for persistence
  • Resetting variables each time defeats counting
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