1.2.1350. Use const

The const keyword may be used to define constant, just like the define() function.

When defining a constant, it is recommended to use ‘const’ when the features of the constant are not dynamical (name or value are known at compile time). This way, constant will be defined at compile time, and not at execution time.

define() function is useful when the constant is not known at compile time, or when case sensitivity is necessary.

<?php
  //Do
  const A = 1;
  // Don't
  define('A', 1);

?>

See also Syntax.

1.2.1350.1. Suggestions

  • Use const instead of define()

1.2.1350.2. Specs

Short name

Constants/ConstRecommended

Rulesets

All, Analyze, CE, CI-checks, Coding conventions, Top10

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

Very high

Features

const, define

Examples

phpMyAdmin, Piwigo

Available in

Entreprise Edition, Community Edition, Exakat Cloud