What are sessions in PHP?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid PHP basics — a prerequisite for any developer role.
Answer
A session is a mechanism to store user-specific information on the server across multiple HTTP requests. Because HTTP is stateless, sessions provide continuity between page loads. Start a session with session_start() (must be called before any output). Store data: $_SESSION["user"] = "Alice";. Retrieve data on the next page: echo $_SESSION["user"];. PHP generates a unique session ID (usually stored in a cookie named PHPSESSID) that links the browser to the server-side session data. Destroy a session: session_destroy(). Sessions are more secure than cookies for storing sensitive data (like user IDs after login) because the data lives on the server, not in the browser.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex PHP answers easy to follow.