3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $records = [ ['1', '2'], ['3', '9'], ['12', '8' ], ['20', '2' ], ['22', '15' ], ['37', '16' ], ['53', '16' ], ['69', '16' ], ['85', '1' ], ['86', '4' ], ['90', '4' ], ['94', '4' ], ['98', '4' ], ['102', '4', '5' ], ['106+X', 'X'], ['106+X', '5', '10'], ['111+X', 'Y'], ['111+X+Y', '7', '20'], ['118+X+Y', 'Z'], ]; $X = 10; $Y = 15; $Z = 5; foreach ($records as $posLen) { $pos = $posLen[0]; $len = $posLen[1]; // $val = $posLen[2]; // if $len is a character, then it represents a variable. if (!is_numeric($len)) { $len = $$len; } $finalPos = 0; $posArray = explode('+', $pos); foreach($posArray as $posItem) { if (is_numeric($posItem)) { $finalPos += $posItem; } else { $finalPos += $$posItem; } } echo "$finalPos, $len" . PHP_EOL; }
based on VtTOQ
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.0
1, 2 3, 9 12, 8 20, 2 22, 15 37, 16 53, 16 69, 16 85, 1 86, 4 90, 4 94, 4 98, 4 102, 4 116, 10 116, 5 121, 15 136, 7 143, 5