November 23, 2026 - 09:43
PHP Lesson 11 — What Does declare(strict_types=1) Actually Change? Image
PHP Tutorials

PHP Lesson 11 — What Does declare(strict_types=1) Actually Change?

0 Comments

Open a PHP example and the second line often says declare(strict_types=1);. We used it in the task-list lessons too. But what actually changes if we remove it? Does it force every variable to keep the same type?

Let's make the difference visible before treating that line as something to copy automatically. You only need the PHP installation from lesson 1, not a database. Create experiment.php in a new folder, open a terminal there, and run each version with php experiment.php. Replace the file contents between experiments.

Start with a conversion PHP allows

PHP
<?php
function doubleNumber(int $number): int
{
    return $number * 2;
}

echo doubleNumber("5"), PHP_EOL;

The output is 10. The declaration int $number says the function expects an integer. The : int after the parentheses declares an integer return value. Yet the call passes "5", a string, rather than the integer 5. PHP accepts the numeric string by converting it.

This does not mean any string is acceptable. Passing "apple" to this function already produces a type error. We are investigating a value that PHP can convert and therefore accepts without telling us about the conversion.

Enable strict_types in the same file

PHP
<?php
declare(strict_types=1);

function doubleNumber(int $number): int
{
    return $number * 2;
}

echo doubleNumber(5), PHP_EOL;

try {
    echo doubleNumber("5"), PHP_EOL;
} catch (TypeError $error) {
    echo "TypeError", PHP_EOL;
}

Now the output has two lines: 10 and TypeError. The integer works; the string does not. The catch block catches the type error from the second call. We print a short label because the full diagnostic also includes a file path and line numbers. Without the catch, the uncaught error stops execution at that point.

declare is a PHP directive, strict_types is the setting, and 1 enables it. Make it the first statement after the opening <?php tag, with no HTML output or executable statement before it. Save the file as UTF-8 without a BOM so an invisible file prefix does not become output. Comments may appear before the declaration.

It does not lock ordinary variables

PHP
<?php
declare(strict_types=1);

$number = "5";
var_dump($number);

$number = 5;
var_dump($number);

You get string(1) "5" followed by int(5). Reassigning the variable to another type is still allowed. The effect we are studying concerns declared scalar parameter and return types, such as int, float, string, and bool. One useful exception: an integer is still accepted where a float is declared, even in strict mode.

A return declaration is a promise too

PHP
<?php
declare(strict_types=1);

function taskCount(): int
{
    return "5";
}

try {
    var_dump(taskCount());
} catch (TypeError $error) {
    echo "Return TypeError", PHP_EOL;
}

There are no parameters, but this still fails. The function promises an integer and attempts to return a string, so it prints Return TypeError. Change return "5"; to return 5; and run it again. You should see int(5). For return-value checking, the strict setting in the file that defines the function matters.

What about numbers submitted by a browser?

Ordinary form fields and query parameters usually arrive as strings. Enabling strict mode does not validate them. Casting everything to (int) just to avoid an error can hide invalid input: a meaningless string may become zero.

Replace experiment.php with this complete example. Check the input shape first, validate the permitted integer range next, and only then call the typed function.

PHP
<?php
declare(strict_types=1);

function doubleNumber(int $number): int
{
    return $number * 2;
}

$raw = $_GET['count'] ?? '5';

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

$count = filter_var($raw, FILTER_VALIDATE_INT, [
    'options' => ['min_range' => 1, 'max_range' => 100],
]);

if ($count === false) {
    echo "Enter an integer from 1 to 100";
    return;
}

echo doubleNumber($count);

Running it in the terminal prints 10 using the default. For a browser test, start php -S 127.0.0.1:8001 in this new folder and open http://127.0.0.1:8001/experiment.php?count=7. The output is 14. Try count=apple, count=0, and count[]=7. The first two fail integer validation; the last is an array and fails the input-type check. Stop the local server with Ctrl+C when finished.

Does one declaration enable it across the project?

No. Create three files in the same folder to see the boundary. The function lives in numbers.php:

PHP
<?php
declare(strict_types=1);

function doubleNumber(int $number): int
{
    return $number * 2;
}

Create caller-weak.php:

PHP
<?php
require __DIR__ . '/numbers.php';

echo doubleNumber("5"), PHP_EOL;

php caller-weak.php prints 10. Strict mode in the function's file does not make this caller strict. Now create caller-strict.php:

PHP
<?php
declare(strict_types=1);

require __DIR__ . '/numbers.php';

try {
    echo doubleNumber("5"), PHP_EOL;
} catch (TypeError $error) {
    echo "Caller TypeError", PHP_EOL;
}

php caller-strict.php prints Caller TypeError. For these parameter conversions, the calling file controls the behavior. Use a consistent setting when starting new files. In an existing application, test inputs and call sites as you adopt it: adding the declaration does not repair the incompatible values already being passed.

Where this appeared in the task list: PHP Lesson 04 — Organize the Code with Functions and Files

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment