3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nearest(array $arr, $needle) { $closest = current($arr); foreach ($arr as $val) { if ($val === $needle) { return $val; // found it :) } if ($val > $closest && $val < $needle) { // we found something closer and less than the needle, so use it $closest = $val; } } // we've reached the end, so return the closest solution return $closest; } $array = array(2012, 2013, 2015, 2016, 2018); $needle = "1970"; print nearest($array, $needle);
Output for git.master, git.master_jit, rfc.property-hooks
2012

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
54.82 ms | 401 KiB | 8 Q