How do check if a PHP session is empty?

How do check if a PHP session is empty?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Distinction Between Unset and Empty in PHP Sessions

Checking whether a session variable exists versus checking whether it holds a value is one of those subtle distinctions that trips up developers new to PHP, yet the underlying logic is straightforward once you understand how the superglobal behaves. The code snippet provided in your source material attempts to handle both cases simultaneously:

if (($_SESSION['something'] == '') || (!isset($_SESSION['something'])) {
    echo 'the session is either empty or doesn\'t exist';
}

While this works, it conflates two distinct states: a variable that has never been set, and a variable that was explicitly set to an empty string. Relying on == for the first condition is also risky because it treats null, 0, "false", and "" as equivalent. This can lead to unexpected logic branches if your application relies on falsy values being treated differently from an absence of data.

The most robust approach separates these concerns completely. First, use isset() or the null coalescing operator to determine existence. Only if you need to distinguish between "not set" and "set to empty string" should you perform a secondary check on the value itself.

Using isset() for Existence Checks

The isset() function is specifically designed to answer one question: "Does this key exist in the array, and does it not hold the value null?" If the session key has never been written to, or if you explicitly assigned null to it, isset() returns false.

If your goal is simply to know if a user has ever interacted with a specific feature (e.g., "Has the user completed the onboarding wizard?"), checking for existence is often sufficient. If the key doesn't exist, assume the default state (usually false or null). This avoids the need for complex conditional chains:

if (!isset($_SESSION['user_completed_onboarding'])) {
    // Key does not exist. Treat as default state.
    $completed = false;
} else {
    // Key exists and is likely a boolean true/false or a timestamp.
    $completed = $_SESSION['user_completed_onboarding'];
}

Handling Empty Strings Explicitly

Your original code checks $_SESSION['something'] == ''. This implies that having an empty string is a valid state that you want to treat as "empty." However, mixing this with isset() can be fragile. Consider this scenario:

  1. User visits page A; the variable is unset.
  2. User visits page B; the script initializes $variable = ''.

In step 2, isset() returns true, but your value check sees an empty string. If you combine them with || (OR), you handle both cases, but it's worth noting that initializing a session variable to an empty string is often a design smell. It suggests the application state might be ambiguous.

A cleaner pattern for handling "empty or unset" specifically when empty strings are meaningful data is:

if (!isset($_SESSION['something']) || $_SESSION['something'] === '') {
    // Handles both: key missing, OR key present but empty string.
    echo 'the session value is effectively empty';
}

Note the use of === for the string comparison. This ensures you aren't accidentally triggering on variables set to null, 0, or "false". If your business logic requires distinguishing between an empty string and a numeric zero, strict equality (===) is mandatory.

The Laravel Context

If you are building an application using the Laravel framework, this manual session manipulation is rarely necessary. Laravel provides helper functions that abstract these checks away. For instance, old() handles input fields, while form request validation determines if a value is present and valid without needing to peek into the raw $_SESSION array directly.

For more granular control over session data in Laravel, you can still access session('key'). This helper method returns null if the key doesn't exist or if it holds null, but crucially, it does not return an empty string unless that is what was explicitly stored. To check for emptiness in a Laravel view or controller:

if (session('something') === null || session('something') === '') {
    // Handle absence or explicit empty state
}

This abstraction reduces the risk of relying on the raw $_SESSION superglobal, which is prone to race conditions and debugging difficulties in complex architectures.

Summary of Best Practices

  1. Prefer existence checks first: Use isset() or the null coalescing operator (??) to verify a key exists before accessing it.
  2. Use strict comparison for values: When checking if a value is an empty string, use === '' to avoid type juggling pitfalls with numbers or booleans.
  3. Avoid mixing logic: Don't rely on == to check for emptiness alongside isset(). Separate the existence check from the value validation for clarity and safety.

By treating "unset" and "empty string" as distinct states, you write code that behaves predictably regardless of how the session data was initialized or modified during runtime.

Our network