Thuta Learning
PHP
IntermediateProgrammingbeginner

Type Declarations & Strict Types

What you'll walk away with

  • Explain the PHP runtime and web behavior behind Type Declarations & Strict Types
  • Test normal, boundary, invalid, and failure cases
  • Write secure and maintainable PHP

Strict mode rejects coercible scalar arguments at calls made from a strict file.

Build a Complete Mental Model

PHP supports scalar, nullable, union, intersection, and class type declarations. `declare(strict_types=1)` changes scalar calls on a per-file, caller-sensitive basis and does not replace input validation.

Apply It in Real PHP

Run and test an original Type Declarations & Strict Types example with normal, boundary, invalid, and failure inputs, recording output, warnings, exceptions, and side effects.

After This Lesson

php
<?php
declare(strict_types=1);
function total(int $qty, float $price): float {
    if ($qty < 0 || $price < 0) throw new InvalidArgumentException('positive values required');
    return $qty * $price;
}
echo total(3, 19.5);
You should see
58.5

Try It Yourself

Run and test an original Type Declarations & Strict Types example with normal, boundary, invalid, and failure inputs, recording output, warnings, exceptions, and side effects.

Security and Common Mistake

Strict scalar behavior depends on the calling file, not merely the declaration file.

PHP Type DeclarationsPHP Documentation Group

Easy traps

  • Strict scalar behavior depends on the calling file, not merely the declaration file.
  • Assuming one sample output proves input, output, authorization, and every failure path are correct.

Hands-on Exercise

Run and test an original Type Declarations & Strict Types example with normal, boundary, invalid, and failure inputs, recording output, warnings, exceptions, and side effects.

You'll know it worked when: 58.5