PHP VERSION: 8.2.18

abstract_syntax_dereferencing.php

<?php
// example of PHP dereferencing

$a = [1,2,3];
$b 'a';

// use $$
var_dump($$b);

// use {}
var_dump(${$b});

// in a function
function test()
{
    
$a = array();
    for (
$x 0$x 3$x++) {
        
$a[] = $x;
    }
    return 
$a;
}

// available after php 5.4
echo test()[2];

Output


array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
2
SOURCE CODE