3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getDigit($Nth){ if($Nth < 10) return $Nth; $no_of_digits = 1; $current_contribution = 9; $actual_length = 9; $prev_length = 0; $starting_number = 1; $power_of_10 = 1; while($actual_length < $Nth){ $no_of_digits++; $current_contribution *= 10; $prev_length = $actual_length; $actual_length += ($current_contribution * $no_of_digits); $power_of_10 *= 10; $starting_number *= 10; } $Nth = $Nth - $prev_length; $offset = $Nth % $no_of_digits === 0 ? intval($Nth / $no_of_digits) - 1 : intval($Nth / $no_of_digits); $number = strval($starting_number + $offset); for($i=1;$i<=$no_of_digits;++$i){ if(($Nth - $i) % $no_of_digits === 0){ return $number[$i-1]; } } } for($i=1;$i<=100;++$i){ echo getDigit($i),PHP_EOL; }
Output for 7.1.25 - 7.1.31, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5

preferences:
106.85 ms | 1499 KiB | 4 Q