PHP strict_types

Last updated on

PHP strict_types is a powerful declaration that transforms the way PHP handles type checking in functions. Introduced in PHP 7, this feature ensures a higher degree of accuracy in type declarations, significantly contributing to code reliability.

In this guide, we’ll delve into the syntax of PHP strict_types and explore its impact on your codebase.

PHP strict_types is a declaration designed to enforce precise type checking in PHP functions. When you set it to 1, it mandates strict mode to match type declarations exactly, leaving no room for type coercion. This added strictness enhances code robustness and reduces the chances of runtime errors related to type inconsistencies.

Now, let’s take a closer look at the syntax of PHP strict_types.

PHP strict_types Syntax

In PHP, the syntax for strict_types is straightforward. It is typically placed at the beginning of a script or file, setting the tone for the entire codebase. Here’s an example:

<?php
declare(strict_types=1);

// Rest of the PHP code follows...

Breaking down the syntax, we find two key elements:

The declare Keyword:

The declare keyword is a versatile tool in PHP, allowing the setting of various directives. In the context of strict_types, it is used to enable or disable behaviors related to type checking.

The strict_types=1 Directive:

This specific declaration under the declare statement, when set to 1, activates strict type checking for the entire script:

<?php
declare(strict_types=1);

For those scenarios where strict type checking is not required, setting it to 0 can disable this feature:

<?php
declare(strict_types=0);

However, it is considered best practice to set strict_types to 1 to harness the full benefits of this feature.

To ensure global application, the declare statement with strict_types should be positioned at the very beginning of the PHP script, preceding any other code.

Let’s move into the section below to understand how Scalar Type Declarations work with strict_types.

Scalar Type Declarations with PHP strict_types

One of the key areas where strict_types shines is with scalar types—integers, floats, strings, and booleans. Pairing scalar type declarations with strict_types fortifies your code against subtle type-related bugs.

Consider this example:

<?php
declare(strict_types=1);

function multiply(int $a, float $b): float {
    return $a * $b;
}

// Calling the function with precise scalar types
$result = multiply(5, 2.5);

Here, the multiply function expects an integer $a and a float $b. If you attempt to call it with mismatched types, say a string and an integer, strict_types springs into action, throwing a TypeError:

<?php

// Calling the function with mismatched types will result in a TypeError
$result = multiply("5", 2);

This preemptive error detection ensures that your functions receive the correct scalar types, mitigating potential runtime disasters.

Let’s summarize it.

Wrapping Up

The introduction of strict_types in PHP 7 has ushered in a new era of precision and reliability in type checking. This powerful declaration transforms how PHP functions handle types, significantly contributing to the overall robustness of code.

In this comprehensive guide, we embarked on a journey to understand the syntax and impact of PHP strict_types. Delving into its syntax, we uncovered its straightforward yet impactful structure. The use of the declare keyword, coupled with the strict_types=1 directive, sets the stage for a script-wide commitment to exact type matching. While the option to disable strict type checking exists, the prevailing best practice is to embrace the benefits by setting strict_types to 1.

Moving forward, we explored the dynamic interplay of scalar type declarations with strict_types. Scalar types, including integers, floats, strings, and booleans, gain enhanced reliability when paired with strict_types. The guide provided practical examples, illustrating how this combination acts as a vigilant guardian against subtle type-related bugs. The preemptive error detection offered by strict_types ensures that functions receive the correct scalar types, mitigating potential runtime disasters.

Share on: