PHP is BROKEN on CodeChef

  1. I have 15+ years’ experience programming in dozens of languages

  2. I have spent ~4 hours searching Google and the Codechef forums for a solution. Please don’t respond with “read the manual”

As far as I can tell, Codechef is broken when submitting PHP code for any output that’s more than one line long.

Although the system DOES work for a single line of output, as demonstrated by, anything beyond that returns either “NZEC” or “Wrong Answer” for EVERY possible combination of input and output methods, for EVERY problem I’ve tried, even though ALL of my solutions work on my local machine.

The following strategies fail in all cases:


$in = fopen(STDIN, “r”);
$out = fopen(‘php://stdout’,“w”);

Then read from $in as

$line = trim(fgets($in, 4096));

And output it as:

fwrite($out, sprintf("%s\n", $result));


$line = fgets(STDIN, 3);

Then output



$line = fgets(STDIN, 3);
$out = fopen(‘php://stdout’,“w”);

Then output

fwrite($out, sprintf("%s\n", $result));


Same as above, but being careful not to include a \n after the final line of output

  1. So CodeChef guys, if their actually is a way to use PHP to solve challenges on your site, please post a substantial challenge (multiple lines of input, multiple lines of output) solved in PHP.

Otherwise, do the world a favor and disable PHP on your site. You’re wasting thousands of hours of people’s time every month by posting challenges that are, literally, impossible to get right.


Take a look at this solution. Or this one.

1 Like

Thanks for your answer, OP.

The solutions you posted do indeed work for the given problems. However they don’t work for this one

It looks like this might go deeper than just plugging into Codechef’s IO system. I suspect there might be something interesting going on with how the solutions are tested against the problem author’s answer files …perhaps Unix vs Windows EOL characters.

I should have given up on this hours ago, but I’m gong to try some other things and report back.

Alright, I’m giving up.

This is just too time consuming to debug because I can’t see either the test cases or the output from the interpreter. Its literally a black box.

If you want to practice coding in PHP, use this site:

Really good problems, and its so much easier to work with, because you can see the output from your code side-by-side with the input that was sent to your code by the test system (just click on any errors in the results list)

@foxly Codechef uses the same SPOJ judge to judge its solutions. errors displayed are also almost same. You can view details about codechef errors here :

1 Like

Is there a specific reason why CodeChef can’t display something along the lines of:

= = = = = = = = =
Input: AAAA
Expected Output: “XXXX”
Your Output: “YYYY”

…when a user’s code fails?

I can understand why you might want to keep the tests “secret” during competitions, but if the goal of CodeChef is to help people build their programming skills, they’d learn an awful lot faster if they had meaningful debugging info.

1 Like

There are a number of ways to help people build programming skills other then displaying test cases. I would suggest you see this :

1 Like

I don’t get it. You’re talking about COINS but successfully solved it yourself.

You’re telling PHP is broken in general, but there are a huge number of AC answers (29 pages), for various problems.

Can you explain what exactly is the problem you’re trying to cope with ?


I seems you charcked it, let us know where the problem was, to help other interested in PHP :wink:

@foxly: That is a harsh criticism that you have made. We believe that there is no issue with the way we execute PHP on our program checker machine. However we may be wrong. Let me explain. As mentioned by @kcahdog we use the same judge that SPOJ use for checking out the programs. All the standard PHP I/O functions are supported in our PHP environment without any errors. Also from the I/O examples that you have mentioned above all of them should work without producing any error, well except #1, which has error in the code.

Please don’t associate Wrong Answer or Run Time Exception(NZEC) with broken PHP environment on CodeChef unless we can nail it down. We agree that there should be better sample solutions and our FAQ page may be improved to better cater to new users. However, there is only so much that we can do.But, since our FAQ page is a wiki page, we also expect the community to help us improve it. Since you have spent so much time on this issue, it will be great help to the community if you can devote a few more minutes in documenting this in the FAQ. In case you face any issues in doing so, please let us know.

1 Like
<?php fscanf(STDIN,"%d\n",$t); if($t<=5 && $t>=1) { for($z=0;$z<$t;$z++) { fscanf(STDIN,"%d%d\n",$chef,$query); if($chef<=20000 && $chef>=1) { if($query<=500 && $query>=1) { $a=(fgets(STDIN)); $chefmoti=explode(" ",$a); if(count($chefmoti)<=10^18 && count($chefmoti)>=1 && count($chefmoti)==$chef) { $b=(fgets(STDIN)); $chefsati=explode(" ",$b); } else { exit; } if(count($chefsati)<=10^18 && count($chefsati)>=1 && count($chefsati)==$chef) { for($z1=0;$z1<$query;$z1++) { fscanf(STDIN,"%d\n",$low); if($low<=10000 && $low>=1) { $sum=array(0); $i=$j=0; while($i<$chef) { while($j<$chef) { $sum[]=$chefmoti[$i]+$chefsati[$j]; $j++; } $j=0; $i++; } sort($sum); echo $sum[$low]; echo "\n"; } else{exit;} } } else {exit;} } } } } ?>

nzec error

try{} catch (Exception $e){echo"Error found";} display no error but codechef display nzec error…

@php_shailesh: Your submission is not meeting the memory limits for this problem and hence the judge needs to kill the running of the submission. You need to optimize your implementation to use less memory. Please refer to the editorial of this problem to get better ideas.