November 23, 2026 - 09:43
PHP Lesson 14 — == vs ===: Why Does a Found Item Look Missing? Image
PHP Tutorials

PHP Lesson 14 — == vs ===: Why Does a Found Item Look Missing?

0 Comments

The task you are searching for is in the array, right at the beginning, yet your check says “not found.” Before blaming the search function, inspect what the returned 0 actually means.

PHP's == compares values while allowing type conversion. === requires both the value and type to match. That extra character becomes important around zero, false, and numbers received as strings.

Predict five comparisons

Create experiment.php in a new folder and run it there with php experiment.php. Before running it, write down what you expect each line to produce.

PHP
<?php
declare(strict_types=1);

var_dump(5 == "5");
var_dump(5 === "5");
var_dump(5 === 5);
var_dump(0 == false);
var_dump(0 === false);

The results are true, false, true, true, and false. Integer 5 and string “5” compare equal loosely, but their types differ. The last two comparisons show that integer zero and boolean false are not identical values.

The file enables strict_types, yet 5 == "5" still evaluates to true. That setting does not turn == into ===. Parameter conversion for typed functions and the comparisons in this example are separate mechanisms.

Why did the first item disappear?

PHP
<?php
$tasks = ['Read', 'Try', 'Change'];
$index = array_search('Read', $tasks, true);

var_dump($index);

if (!$index) {
    echo "Wrong check: not found", PHP_EOL;
}

if ($index === false) {
    echo "Not found", PHP_EOL;
} else {
    echo "Found at index ", $index, PHP_EOL;
}

The first output is int(0). The example array starts at index zero, so Read really is there. But if (!$index) does not specifically ask whether the search failed. It evaluates the value as a boolean. Zero is false-like, so the wrong message appears.

The later $index === false checks for the exact value that means the search failed. Integer zero is not boolean false, so the example correctly prints Found at index 0. Change the search string to Missing. It now returns false and the final check prints Not found.

The third argument true in array_search() has a different job: it asks the search to compare both value and type. It does not replace checking the return value with === false. One setting controls how the search works; the later comparison interprets its result.

Is a submitted 1 the same type as integer 1?

An ordinary form field usually supplies string '1', so comparing it directly with integer 1 using === can fail. Rather than guessing at the type, define which input values you accept first.

PHP
<?php
$raw = $_POST['done'] ?? '0';

if (!is_string($raw) || !in_array($raw, ['0', '1'], true)) {
    echo "Invalid state";
    return;
}

$done = $raw === '1';
var_dump($done);

In the terminal the default '0' produces bool(false). The string check rejects arrays. Strict in_array(..., true) accepts only string '0' and string '1'. The last expression turns that validated input into a boolean the application can use.

For a web test, run php -S 127.0.0.1:8001. In a second PowerShell window run curl.exe -d "done=1" http://127.0.0.1:8001/experiment.php; expect bool(true). On macOS and Linux use curl instead of curl.exe. Submitting done=2 or done[]=1 should produce Invalid state. The example only inspects input; it does not update a stored task.

A single equals sign changes the value

PHP
<?php
$done = false;

if ($done = true) {
    echo "Assignment changed the value", PHP_EOL;
}

var_dump($done);

= is assignment, not comparison. We set the originally false $done to true, and the condition evaluates that assigned value. The message appears and the final line shows bool(true).

Replace the condition with if ($done === true) while leaving the initial $done = false; unchanged. Run it again: the message should disappear and the only output should be bool(false).

Where this appeared in the task list: PHP Lesson 07 — Change a Task's Completion Status

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment