# RFC: Constant Scalar Expressions

- Status: Withdrawn
- Authors: Anthony Ferrara ircmaxell@php.net
- Date: 2013-08-13
- Version: 1.0
- Original: https://wiki.php.net/rfc/const_scalar_expressions
- Page: https://externals.io/rfc/const_scalar_expressions
- 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

## Discussions on the mailing list

- [\[RFC\] Constant Scalar Expressions](https://externals.io/message/68500.md) — started on 2013-08-13, 29 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.

## Proposal

Adding parser support for compile-time resolved scalar expressions.

### 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
- *.* - Concat

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

### Constant Declarations

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

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

Note that constants are not supported within a declaration (since this happens at compile time). So *const BIZ = FOO &lt;&lt; BAR;* would be a compile error.

### Class Constant Declarations

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

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

### Class Property Declarations

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

```file
<?php
class Foo {
    public $foo = 1 + 1;
    public $bar = [
        1 + 1,
        1 << 2,
        "foo " . 
            "bar"
    ];
    public $baseDir = __DIR__ . "/base";
}
?>
```

### Function Argument Declarations

[function\_argument\_declarations.php](https://wiki.php.net/_export/code/rfc/const_scalar_expressions?codeblock=3)

```file
<?php
function foo($a = 1 + 1, $b = 2 << 3) {
}
?>
```

### Static Variable Declarations

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

```file
<?php
function foo() {
    static $a = 1 + 1 
    static $b = [ 1 << 2 ];    
}
?>
```

## Backward Incompatible Changes

None

## Proposed PHP Version(s)

PHP 5.NEXT

## SAPIs Impacted

None

## Impact to Existing Extensions

None

## Impact to Opcode Caching

None, as this is a pure compile-time construct, the generated op-array is the same as if there was no expression (it's purely static).

## New Constants

None

## php.ini Defaults

None

## Patches and Tests

An implementation based off of current master is available: [Implementation On GitHub](https://github.com/ircmaxell/php-src/tree/static_operation_parsing) ([Diff On GitHub](https://github.com/ircmaxell/php-src/compare/static_operation_parsing))

This patch is ready to be merged with tests.
