Consider this PHP script that uses sessions. What will it output?
<?php session_start(); if (!isset($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } echo $_SESSION['count']; ?>
Think about what happens the first time the session variable is set.
The code starts a session and checks if 'count' exists in the session. If not, it sets it to 1. Then it prints the value. On the first run, it prints 1.
Choose the correct explanation of what session_start() does in PHP.
Think about how PHP knows which session belongs to which user.
session_start() either creates a new session or resumes an existing one by checking the session ID sent by the client, usually via a cookie.
Look at this PHP code snippet. It causes a warning about headers already sent. Why?
<?php session_start(); echo "Hello"; session_start(); ?>
Headers must be sent before any output. What happens if you call session_start() after output?
Calling session_start() sends HTTP headers. If output is sent before, headers cannot be sent again, causing a warning.
Choose the correct way to set a session variable named user with value admin.
Remember the superglobal array used for sessions.
Session variables are stored in the $_SESSION superglobal array. The other options are invalid syntax or functions.
Given this PHP code, how many session variables exist after execution?
<?php session_start(); $_SESSION['a'] = 1; $_SESSION['b'] = 2; unset($_SESSION['a']); $_SESSION['c'] = 3; ?>
Think about what unset() does to session variables.
The code sets three session variables but removes 'a' with unset(). So only 'b' and 'c' remain, totaling 2.