Bird
0
0

Find the bug in this PHP code snippet:

medium📝 Debug Q7 of 15
PHP - Sessions and Cookies
Find the bug in this PHP code snippet:
<?php
session_start();
if($_SESSION['logged_in'] = true) {
  echo 'Welcome!';
}
?>
AAssignment used instead of comparison in if condition
BMissing semicolon after session_start()
CEcho statement syntax error
DSession variable name invalid
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if condition

    The code uses = (assignment) instead of == (comparison).
  2. Step 2: Understand effect of assignment in if

    Assignment always returns true, so condition is always true, which is likely a bug.
  3. Final Answer:

    Assignment used instead of comparison in if condition -> Option A
  4. Quick Check:

    Use == to compare, not = [OK]
Quick Trick: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = in if instead of ==
  • Missing semicolons
  • Wrong variable names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes