Bird
Raised Fist0
PHPprogramming~20 mins

Session variables in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Session 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 session code?

Consider the following PHP code that uses session variables. What will be printed?

PHP
<?php
session_start();
$_SESSION['count'] = 1;
$_SESSION['count'] += 1;
echo $_SESSION['count'];
?>
A2
B1
C3
DUndefined index error
Attempts:
2 left
💡 Hint

Remember that session variables keep their values during the session and you can update them like normal variables.

❓ Predict Output
intermediate
2:00remaining
What error does this PHP session code produce?

What error will this PHP code produce when run?

PHP
<?php
$_SESSION['user'] = 'Alice';
echo $_SESSION['user'];
?>
AWarning: session_start() not called
BNo error, outputs 'Alice'
CFatal error: Undefined variable 'SESSION'
DParse error: syntax error
Attempts:
2 left
💡 Hint

Session variables require a special function to be called before using them.

🔧 Debug
advanced
3:00remaining
Why does this PHP session code not keep the value?

This PHP code tries to count page visits using a session variable but always prints 1. Why?

PHP
<?php
session_start();
if (!isset($_SESSION['visits'])) {
    $_SESSION['visits'] = 1;
} else {
    $_SESSION['visits'] = 1;
}
echo $_SESSION['visits'];
?>
AThe variable 'visits' is not initialized before use
BThe else block resets 'visits' to 1 every time, so it never increments
CPHP sessions are disabled on the server
Dsession_start() is missing, so session variables don't work
Attempts:
2 left
💡 Hint

Look carefully at what happens inside the else block.

❓ Predict Output
advanced
2:00remaining
What is the output of this PHP session code with unset?

What will this PHP code output?

PHP
<?php
session_start();
$_SESSION['name'] = 'Bob';
unset($_SESSION['name']);
echo isset($_SESSION['name']) ? $_SESSION['name'] : 'No name';
?>
AEmpty string
BBob
CUndefined index error
DNo name
Attempts:
2 left
💡 Hint

What does unset() do to a session variable?

❓ Predict Output
expert
3:00remaining
What is the output of this PHP session code with session_destroy?

What will this PHP code output?

PHP
<?php
session_start();
$_SESSION['color'] = 'red';
session_destroy();
echo isset($_SESSION['color']) ? $_SESSION['color'] : 'No color';
?>
AWarning: session has been destroyed
BNo color
Cred
DEmpty string
Attempts:
2 left
💡 Hint

Does session_destroy() immediately clear the $_SESSION array in the current script?

Practice

(1/5)
1. What is the main purpose of using session_start() in PHP?
easy
A. To connect to a database
B. To send output to the browser
C. To begin a session and enable session variables
D. To include another PHP file

Solution

  1. Step 1: Understand what session_start() does

    This function initializes a session or resumes the current one based on a session identifier passed via a cookie or URL.
  2. Step 2: Identify the purpose of session variables

    Session variables store user data across multiple pages, but they only work if a session is started first.
  3. Final Answer:

    To begin a session and enable session variables -> Option C
  4. Quick Check:

    session_start() = start session [OK]
Hint: Remember: session_start() must be called before using $_SESSION [OK]
Common Mistakes:
  • Thinking session_start() connects to a database
  • Using session variables without calling session_start()
  • Confusing session_start() with output functions
2. Which of the following is the correct way to set a session variable named user with the value "Alice"?
easy
A. $_SESSION['user'] = 'Alice';
B. session['user'] = 'Alice';
C. set_session('user', 'Alice');
D. session_start['user'] = 'Alice';

Solution

  1. Step 1: Recall the syntax for setting session variables

    Session variables are stored in the global $_SESSION array, so you assign values like $_SESSION['key'] = value;.
  2. Step 2: Check each option's syntax

    $_SESSION['user'] = 'Alice'; uses the correct $_SESSION superglobal with proper array syntax and assignment.
  3. Final Answer:

    $_SESSION['user'] = 'Alice'; -> Option A
  4. Quick Check:

    $_SESSION['key'] = value [OK]
Hint: Use $_SESSION['name'] = value to set session data [OK]
Common Mistakes:
  • Using session without the $_SESSION superglobal
  • Trying to call session_start as an array
  • Using undefined functions like set_session()
3. What will be the output of the following PHP code?
session_start();
$_SESSION['count'] = 1;
$_SESSION['count']++;
echo $_SESSION['count'];
medium
A. 1
B. 0
C. Error
D. 2

Solution

  1. Step 1: Analyze session variable initialization and increment

    The code sets $_SESSION['count'] to 1, then increments it by 1 using ++.
  2. Step 2: Determine the final value of $_SESSION['count']

    After increment, the value becomes 2, which is then printed by echo.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    1 incremented by 1 = 2 [OK]
Hint: Increment session variable with ++ after setting it [OK]
Common Mistakes:
  • Forgetting to call session_start() before using $_SESSION
  • Assuming $_SESSION['count'] stays 1 after ++
  • Expecting an error due to session usage
4. Identify the error in this PHP code snippet:
$_SESSION['user'] = 'Bob';
echo $_SESSION['user'];
medium
A. Missing session_start() before using $_SESSION
B. Incorrect array syntax for $_SESSION
C. Cannot assign string to $_SESSION
D. Echo statement syntax is wrong

Solution

  1. Step 1: Check for session initialization

    The code uses $_SESSION but does not call session_start() first, which is required to access session variables.
  2. Step 2: Verify syntax correctness

    The array syntax and echo statement are correct, and assigning a string to $_SESSION is valid.
  3. Final Answer:

    Missing session_start() before using $_SESSION -> Option A
  4. Quick Check:

    Always call session_start() before $_SESSION [OK]
Hint: Always start session with session_start() before $_SESSION use [OK]
Common Mistakes:
  • Using $_SESSION without session_start()
  • Thinking $_SESSION needs special syntax
  • Assuming echo syntax is incorrect
5. You want to count how many times a user visits a page during a session. Which code snippet correctly implements this using session variables?
hard
A.
session_start();
$_SESSION['visits']++;
echo $_SESSION['visits'];
B.
session_start();
if (!isset($_SESSION['visits'])) {
  $_SESSION['visits'] = 1;
} else {
  $_SESSION['visits']++;
}
echo $_SESSION['visits'];
C.
session_start();
$_SESSION['visits'] = 1;
echo $_SESSION['visits'];
D.
if (!isset($_SESSION['visits'])) {
  $_SESSION['visits'] = 1;
} else {
  $_SESSION['visits']++;
}
echo $_SESSION['visits'];

Solution

  1. Step 1: Ensure session is started

    Calling session_start() is necessary to use session variables.
  2. Step 2: Check visit count logic

    The code checks if $_SESSION['visits'] exists; if not, it sets it to 1. Otherwise, it increments the count. This correctly tracks visits.
  3. Step 3: Verify output

    Finally, it echoes the current visit count, showing how many times the user visited during the session.
  4. Final Answer:

    Code snippet with session_start() and visit count check -> Option B
  5. Quick Check:

    Initialize then increment visits with session_start() [OK]
Hint: Always check isset() before incrementing session counters [OK]
Common Mistakes:
  • Forgetting session_start() before using $_SESSION
  • Not checking if session variable exists before increment
  • Resetting visit count every page load
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