# RFC: Const array/string dereference

- Status: Implemented (PHP 5.5)
- Authors: Xinchen Hui laruence@php.net
- Date: 2011-11-30
- Version: 1.0
- Original: https://wiki.php.net/rfc/constdereference
- Page: https://externals.io/rfc/constdereference
- 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

### do you think it is okey to apply this to TRUNK(not 5.4)? (closed)

yes: 10 · no: 0

Voters: crodas (yes), dm (yes), irker (yes), jwage (yes), kassner (yes), klaussilveira (yes), kriscraig (yes), stas (yes), tyrael (yes), weierophinney (yes)

## Discussions on the mailing list

- [\[RFC\] Const array/string dereference](https://externals.io/message/56676.md) — started on 2011-11-30, 4 messages
- [\[RFC\]Call for voting about const array/string dereference](https://externals.io/message/56901.md) — started on 2011-12-13, 19 messages

---

## Introduction

A little improvement to make things consistent.

## Proposal

Base on feature request: <https://bugs.php.net/bug.php?id=60408>:

```
Please add array constructor dereferencing support

like JavaScript, C# and all languages excluding PHP 5.4

<?php

 $i=0;
 print(array('pa','th')[$i++]); // pa
 print(['wa','ss'][$i++]); // ss

?>
```

I wrote a patch to make php support const array/string dereferencing. After the patch, the following script works:

```php
echo array(1, 2, 3)[0]; //output 1
echo "foobar"[2]; //output o
echo "foobar"["foo"][0] // output f
 
echo [1,3,4][2]; //output 4
```

## Vote

**Vote: do you think it is okey to apply this to TRUNK(not 5.4)?** — yes: 10 · no: 0 (closed)

## Patches

- <https://gist.github.com/2375008>

## Tests

- [http://www.laruence.com/php-dev/const\_dereference\_001.phpt](http://www.laruence.com/php-dev/const_dereference_001.phpt)
- [http://www.laruence.com/php-dev/const\_dereference\_002.phpt](http://www.laruence.com/php-dev/const_dereference_002.phpt)
- [http://www.laruence.com/php-dev/const\_dereference\_003.phpt](http://www.laruence.com/php-dev/const_dereference_003.phpt)

## Changelog

- 2011-11-30 Xinchen Hui: Initial RFC creation
- 2011-12-13 Xinchen Hui: Start voting
- 2012-04-13 Xinchen Hui: Update patch, fixed issue spot by Dmitry
- 2012-04-13 Xinchen Hui: Close voting
- 2012-04-17 Xinchen Hui: Committed &amp;&amp; Close
