$input = fgets(STDIN); $str = explode(" ", $input); $from = $str[0]; $to = $str[1]; for ($i = $from; $i <= $to; $i++) { if (($i % 3 == 0) || (preg_match('/^3/', $i))) { echo $i; } }