# RFC: Constant Scalar Expressions (with constants)

- Status: Implemented (PHP 5.6)
- Authors: Bob Weinand bwoebi@php.net
- Date: 2013-11-03
- Original: https://wiki.php.net/rfc/const_scalar_exprs
- Page: https://externals.io/rfc/const_scalar_exprs
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Should the patch for this RFC be merged into PHP 5.6? (closed)

Yes: 16 · No: 2

Voters: ajf (Yes), bukka (Yes), bwoebi (Yes), datibbaw (Yes), dm (Yes), indeyets (Yes), juliens (No), krakjoe (Yes), laruence (Yes), levim (Yes), mike (Yes), pierrick (Yes), reeze (Yes), seld (Yes), treffynnon (Yes), tyrael (Yes), willfitch (Yes), yohgaki (No)

## Discussions on the mailing list

- [\[RFC\] Constant Scalar Expressions (with constants)](https://externals.io/message/70048.md) — started on 2013-11-07, 14 messages
- [\[RFC\] \[VOTE\] Constant scalar expressions](https://externals.io/message/70238.md) — started on 2013-11-20, 16 messages
- [constant scalar expressions](https://externals.io/message/71939.md) — started on 2014-02-01, 3 messages

---

## 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](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=0)

```file
<?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
- *&amp;* - Bitwise AND
- *^* - Bitwise XOR
- *&lt;&lt;* - Bitwise Shift Left
- *&gt;&gt;* - Bitwise Shift Right
- *.* - Concatenation
- *?:* - Ternary Operator
- *&lt;=* - Smaller or Equal
- *=&gt;* - Greater or Equal
- *==* - Equal
- *!=* - Not Equal
- *&lt;* - Smaller
- *&gt;* - Greater
- *===* - Identical
- *!==* - Not Identical
- *&amp;&amp;* / *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
- *&lt;&lt;&lt;HEREDOC* - HEREDOC string syntax (without variables)
- *&lt;&lt;&lt;'NOWDOC'* - NOWDOC string syntax
- *SOME\_RANDOM\_CONSTANT* - Constants
- *class\_name::SOME\_CONST* - Class constants

### Constant Declarations

[constant\_declarations.php](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=1)

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

### Class Constant Declarations

[class\_constant\_declarations.php](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=2)

```file
<?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](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=3)

```file
<?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](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=4)

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

### Static Variable Declarations

[static\_variable\_declarations.php](https://wiki.php.net/_export/code/rfc/const_scalar_exprs?codeblock=5)

```file
<?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](https://github.com/bwoebi/php-src/tree/const_scalar_exprs) ([Diff On GitHub](https://github.com/bwoebi/php-src/compare/const_scalar_exprs))

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.
