Take the State of PHP 2026 Survey →

Constant Scalar Expressions (with constants) View as Markdown

Implemented (PHP 5.6)
Authors
Bob Weinand bwoebi@php.net
Date
2013-11-03
Source
wiki.php.net/rfc/const_scalar_exprs

Vote

Should the patch for this RFC be merged into PHP 5.6? closed
Yes
16
No
2
18 voters
ajf Yes 2013-11-20 20:58
bukka Yes 2013-11-21 09:38
bwoebi Yes 2013-11-20 16:53
datibbaw Yes 2013-11-22 18:22
dm Yes 2013-11-20 17:58
indeyets Yes 2013-11-20 16:57
juliens No 2013-11-23 22:21
krakjoe Yes 2013-11-21 09:00
laruence Yes 2013-11-21 03:18
levim Yes 2013-11-22 16:14
mike Yes 2013-11-22 10:01
pierrick Yes 2013-11-22 18:40
reeze Yes 2013-11-22 14:04
seld Yes 2013-11-20 17:14
treffynnon Yes 2013-11-25 14:31
tyrael Yes 2013-11-20 21:06
willfitch Yes 2013-11-22 01:32
yohgaki No 2013-11-22 01:19

Discussions on the mailing list

[RFC] Constant Scalar Expressions (with constants)
by Bob Weinand · 14 messages
[RFC] [VOTE] Constant scalar expressions
by Bob Weinand · 16 messages
constant scalar expressions
by Rasmus Schultz · 3 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

This RFC brings static scalar expressions to the parser. This allows places that only take static values (const declarations, property declarations, function arguments, etc) to also be able to take static expressions.

This can allow for writing far easier to understand code, by allowing for far more expressive code.

The main difference to Anthony's RFC is (apart from a few operators more) that constants can be involved in these scalar operations:

const_scalar_exprs_diff.php

<?php
 
const a = 1;
 
const b = a?2:100; // here the value of the constant "b" is dependent on the constant "a"
 
?>

Proposal

Adding parser support for scalar expressions: operations on constants or constant values.

Supported Operations

The following operations are currently supported by this proposal:

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • ///// - Division
  • % - Modulus
  • ! - Boolean Negation
  • ~ - Bitwise Negation
  • | - Bitwise OR
  • & - Bitwise AND
  • ^ - Bitwise XOR
  • << - Bitwise Shift Left
  • >> - Bitwise Shift Right
  • . - Concatenation
  • ?: - Ternary Operator
  • <= - Smaller or Equal
  • => - Greater or Equal
  • == - Equal
  • != - Not Equal
  • < - Smaller
  • > - Greater
  • === - Identical
  • !== - Not Identical
  • && / and - Boolean AND
  • || / or - Boolean OR
  • xor - Boolean XOR

Also supported is grouping static operations: (1 + 2) * 3.

Supported Operands

  • 123 - Integers
  • 123.456 - Floats
  • “foo” - Strings
  • __LINE__ - Line magic constant
  • __FILE__ - File magic constant
  • __DIR__ - Directory magic constant
  • __TRAIT__ - Trait magic constant
  • __METHOD__ - Method magic constant
  • __FUNCTION__ - Function magic constant
  • __NAMESPACE__ - Namespace magic constant
  • <<<HEREDOC - HEREDOC string syntax (without variables)
  • <<<'NOWDOC' - NOWDOC string syntax
  • SOME_RANDOM_CONSTANT - Constants
  • class_name::SOME_CONST - Class constants

Constant Declarations

constant_declarations.php

<?php
const FOO = 1 + 1;
const BAR = 1 << 1;
const GREETING = "HELLO";
const BAZ = GREETING." WORLD!"
?>

Class Constant Declarations

class_constant_declarations.php

<?php
class Foo {
    const FOO = 1 + 1;
    const BAR = 1 << 1;
    const GREETING = "HELLO";
    const BAZ = self::GREETING." WORLD!"
}
?>

Class Property Declarations

class_property_declarations.php

<?php
class Foo {
    const BAZ = 10;
}
 
class Bar {
    public $foo = 1 + 1;
    public $bar = [
        1 + 1,
        1 << 2,
        Foo::BAZ => "foo "."bar"
    ];
    public $baseDir = __DIR__ . "/base";
}
?>

Function Argument Declarations

function_argument_declarations.php

<?php
const BAR = 1;
 
function foo($a = 1 + 1, $b = 2 << 3, $c = BAR?10:100) {
}
?>

Static Variable Declarations

static_variable_declarations.php

<?php
const BAR = 0x10;
 
function foo() {
    static $a = 1 + 1;
    static $b = [1 << 2];
    static $c = 0x01 | BAR;
}
?>

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 5.NEXT

Patches and Tests

An implementation based off of current master is available: Implementation On GitHub (Diff On GitHub)

The patch is ready to be merged. (Opcache support is included, thanks to Dmitry)

Note on implementation

The implementation of the scalar expressions is based on an AST. That AST implementation eventually could be used later as a general-purpose AST for compiler with a few tweaks.

Vote

Vote: Should the patch for this RFC be merged into PHP 5.6? — Yes: 16 · No: 2 (closed)

The vote started the 20th November 2013 and ended the 27th November 2013.