Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Sessions and Cookies
What will be the output of this PHP code?
<?php
session_start();
$_SESSION['role'] = 'admin';
echo $_SESSION['role'];
?>
Asession_start error
Brole
CUndefined index error
Dadmin
Step-by-Step Solution
Solution:
  1. Step 1: Start the session

    The call to session_start() initializes the session or resumes the current one.
  2. Step 2: Assign session variable

    $_SESSION['role'] = 'admin'; sets the session variable 'role' to 'admin'.
  3. Step 3: Output the session variable

    echo $_SESSION['role']; prints the value stored in the session variable, which is 'admin'.
  4. Final Answer:

    admin -> Option D
  5. Quick Check:

    Session variable correctly set and echoed [OK]
Quick Trick: Always call session_start() before accessing $_SESSION [OK]
Common Mistakes:
  • Forgetting to call session_start() before accessing $_SESSION
  • Assuming session variables persist without session_start()
  • Confusing session variable names with their values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes