PHP shorthand for isset()?

PHP shorthand for isset()?

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

Stefan Bogdanescu

Founder & Senior Architect

PHP shorthand for isset()?

The desire to condense conditional assignments into a single line is a common impulse in PHP development. The standard pattern involves checking if a variable exists before assigning it a default value:

if (!isset($var)) {
    $var = "";
}

Developers often look for a syntax like $var = $var | "";, hoping to leverage bitwise operators or logical short-circuiting to achieve the same result. Unfortunately, PHP does not support this shorthand directly. The bitwise OR operator (|) will evaluate both operands regardless of whether the left side is set, and it also treats the string "0" and the boolean false as "falsy" values that would be converted to 0 during evaluation. This behavior makes bitwise operators unsuitable for null coalescing logic.

The language provides a dedicated operator for this exact scenario: the null coalescing operator (??). Introduced in PHP 7, it allows you to assign a value only if the left-hand operand is null. Crucially, this includes cases where the variable has never been defined or where it was explicitly set to null using the assignment operator.

$var = $var ?? "";

This syntax is functionally equivalent to the verbose if (!isset()) block but reads more clearly and executes with slightly less overhead since the conditional logic is handled natively by the interpreter rather than through a control flow statement. If $var contains any value—whether it is a string, number, object, or even an empty array—the expression returns that value unchanged. The default "" is only applied if $var is strictly null.

It is important to distinguish between "unset" and null. Using isset() checks for the existence of a variable key in the scope; it returns false if the variable is unset or if it holds the value null. However, the ?? operator specifically targets the null state. If you are working with legacy code where variables might be unset entirely, ?? will correctly return the default value because an undefined variable evaluates to null in this context.

There is a common pitfall involving loose equality checks or older patterns that rely on isset(). Consider a scenario where you want to ensure a variable isn't empty string as well. The ?? operator alone cannot handle that; it only guards against null. If your requirement involves treating an empty string as a "missing" value, you must combine ?? with additional logic or use the older null coalescing assignment pattern found in specific contexts, though modern PHP encourages explicit checks for empty strings.

$var = $var ?? ""; // Only if $var is null
$var = ($var === "" ? "default" : $var); // If you consider empty string as missing

In frameworks like Laravel, this pattern appears frequently when handling request inputs or configuration values. The framework's old() helper and the input array often return null if no value is provided in a specific session or request context. Using the shorthand ensures that your application doesn't break due to undefined variables while keeping the codebase readable.

$name = $request->input('name') ?? "";
$email = $request->input('email') ?? "default@example.com";

While some might argue that readability is subjective, the null coalescing operator has become a de facto standard for defensive programming in PHP. It eliminates the visual noise of if blocks dedicated solely to initialization and reduces the cognitive load required to parse conditional assignments.

For projects running on older versions of PHP (prior to 7.0), this shorthand is unavailable. In those environments, you must stick to the ternary operator or the traditional isset() check. The ternary approach offers a middle ground for syntax compactness without relying on version-specific features:

$var = isset($var) ? $var : "";

However, this still requires two lines if you want to assign the result back to a variable cleanly, whereas ?? handles the assignment in one expression. When upgrading your PHP stack, migrating from isset() blocks to ?? is often a high-impact refactoring step that improves both performance and code clarity.

Our network