3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Path { public $currentPath; private $array; function __construct($path) { $this->currentPath = $path; $array = explode("/", $path); } public function cd($newPath) { $change = explode("/", $newPath); foreach ($change as $value) { if ($value == "..") { if (count($array) > 0) { array_pop($array); } } else { array_push($array, $value); } } $currentPath = implode("/", $array); } } $path = new Path('/a/b/c/d'); echo $path->cd('../x')->currentPath;

preferences:
37.35 ms | 402 KiB | 5 Q