Skip to content

42: Drop the Third Argument to define()

Register a constant with its canonical spelling. PHP 8.6 deprecates passing the old case-insensitivity argument, even when it is false.

  • PHP 8.6 CLI. Confirm the version with php --version.
  • Review the previous chapter if you’re following the series in order.

Allow about 10 minutes for the walkthrough and exercise.

A runnable define-third-argument.php example.

Run the following from the root of the Code with PHP repository.

Terminal window
php code/php-8-6/42-define-third-argument/define-third-argument.php
define-third-argument.php
<?php
declare(strict_types=1);
define('DEFAULT_CURRENCY', 'AUD');
echo constant('DEFAULT_CURRENCY'), PHP_EOL;
echo 'Lowercase alias exists: ', defined('default_currency') ? 'yes' : 'no', PHP_EOL;
AUD
Lowercase alias exists: no

Use two arguments and read the constant with its declared casing. If your old code passed true, audit consumers for case mismatches as part of the migration.

Check defined('default_currency') after the declaration. Then check defined('DEFAULT_CURRENCY') and compare the results.

Continue with Handle Class Names Explicitly in Type Checks.