November 23, 2026 - 09:43
PHP Lesson 13 — isset, empty and ??: Why Does Zero Count as Empty? Image
PHP Tutorials

PHP Lesson 13 — isset, empty and ??: Why Does Zero Count as Empty?

0 Comments

You enter 0 into a field and the application says it is empty. Enter three spaces elsewhere and it accepts them. PHP is not choosing randomly: our check is answering a different question from the one we meant to ask.

isset(), empty(), and ?? are not three spellings for the same operation. We used related checks while reading task-list input. Let's find exactly where zero gets lost.

Zero exists, so why does empty return true?

Save this as experiment.php in a new folder, open a terminal there, and run php experiment.php. var_dump() displays each boolean result explicitly.

PHP
<?php
$task = ['done' => 0, 'title' => '0'];

var_dump(isset($task['done']));
var_dump(empty($task['done']));
var_dump(isset($task['title']));
var_dump(empty($task['title']));

All four lines say bool(true). The integer 0 and string '0' both exist and are not null, so isset() accepts them. But empty() considers both empty. It does not merely ask whether the field was submitted; it checks whether the variable is missing or has a value PHP considers empty.

That includes false, 0, 0.0, '0', an empty string, an empty array, and null. An undefined variable also returns true without a warning. Sometimes that is useful, such as checking whether a task array is empty. If zero is a valid quantity, position, or completion state, though, write a check that preserves that meaning.

Why are three spaces not empty?

PHP
<?php
$title = '   ';

var_dump(empty($title));
var_dump(trim($title) === '');
var_dump(empty('0'));

The results are false, true, and true. Three spaces form a nonempty string; empty() does not trim it. The expression trim($title) === '' first removes ordinary leading and trailing whitespace and then compares the remaining string with an empty string. We are testing ordinary space characters here, not claiming to remove every Unicode whitespace character.

If a title must contain something after trimming, write that rule directly. A numeric field needs a different decision: is zero allowed, and what is the minimum? One broad emptiness check cannot validate every kind of input.

?? falls back only for a missing or null value

PHP
<?php
$task = ['title' => '', 'count' => 0, 'note' => null];

var_dump($task['title'] ?? 'Untitled');
var_dump($task['count'] ?? 10);
var_dump($task['note'] ?? 'No note');
var_dump($task['missing'] ?? 'Fallback');

The first line keeps the empty string: string(0) "". The second prints int(0), not 10. The explicitly null note produces No note, and the absent key produces Fallback.

So $task['title'] ?? 'Untitled' does not promise to replace an empty title. It chooses the right-hand value when the key is absent or its value is null. For ordinary array-key access it also avoids the undefined-key warning. Whether the result has an acceptable type and content is a separate question.

The key exists, but its value is null

PHP
<?php
$task = ['note' => null];

var_dump(isset($task['note']));
var_dump(array_key_exists('note', $task));
var_dump(isset($task['missing']));
var_dump(array_key_exists('missing', $task));

The output is false, true, false, false. isset($task['note']) fails because the value is null. array_key_exists() asks only whether the key is present. This matters when an update must distinguish “field omitted” from “field explicitly supplied as null.”

Ask a precise question about the title

Put those observations into a small input check. This experiment uses a query parameter so you can change it in the address bar. The task-creation form uses $_POST instead; this example does not write anything to a database.

PHP
<?php
$raw = $_GET['title'] ?? '';

if (!is_string($raw)) {
    echo "Invalid input type";
    return;
}

$title = trim($raw);

if ($title === '') {
    echo "Write a title";
} else {
    echo "Title accepted";
}

Start php -S 127.0.0.1:8001 in the folder. Open http://127.0.0.1:8001/experiment.php?title=0: the response is Title accepted. Using ?title=%20%20%20 submits three spaces and produces Write a title. Using ?title[]=Read submits an array, producing Invalid input type. The fallback ?? '' does not prevent arrays, which is why we check is_string() before trimming.

Finish by trying ?title=Read. Our check establishes that the title contains usable text. Length limits, allowed content, and encoding it when rendered in HTML are further decisions, not things this emptiness check has already solved.

Where this appeared in the task list: PHP Lesson 03 — Read and Validate a Task Form

Official documentation: isset · empty · Null coalescing operator · array_key_exists · trim

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment