3 / 3
Mar 2005

I don't understand why this get's a runtime limit exceeded, I've tried it for myself and it seems just fine...is it just too slow?

<?php
$fourtytwo = false;
while(!$fourtytwo)
{
        $stdin = fopen('php://stdin', 'r');
        $input = fgets($stdin,10000);
        if($input == 42)
                $fourtytwo = true;
        else
                echo $input;
        fclose($stdin);
}
?>
  • created

    Mar '05
  • last reply

    Mar '05
  • 2

    replies

  • 941

    views

  • 2

    users

Move this:

$stdin = fopen('php://stdin', 'r');

fclose($stdin);

out of the main loop. It seems that closing and opening stdin in each iteration is illegal in php (result from fgets is undefined and your program hangs given 50k+ lines)

doh I didn't even think about having them inside the loop...that's just bad coding anyway so thanks wink