Leo Jiang
Leo Jiang

Reputation: 26075

Why does PHP sometimes return a blank page without an error?

I use PHP for programming contests (bad choice, I know. But it's the only language I'm good at). Often, when I create a very large variable, PHP returns a blank page. No errors are generated.

For example, if I have a script like this:

<?php
echo 'test';

$var=array();
for($i=0;$i<9999999999;$i++){
  $var[]=$i*9999999999;
}

No output would be outputted; not even the "test".

Is there a way to prevent this?

P.S. The script usually terminates quickly, in about 2-3 seconds. Which leads me to think that PHP is terminating the script without reaching the end.

Upvotes: 2

Views: 6831

Answers (1)

bretterer
bretterer

Reputation: 5781

More than likely you are getting a

Fatal error: Allowed memory size of 134217728 bytes exhausted

There are a few things that can be done to change this as well as to display the error. One is to either change error reporting in your php.ini file or to add

error_reporting(E_ALL);
ini_set("display_errors", 1);

at the top of your code. The second would be increase the allowed size.

You are using a lot of memory trying to multiply this large of a number that many times. What is the reason you are trying to do this? Is there a different way you might be able to do the same thing?

Here are a few resources you can use

to increase the size http://www.mydigitallife.info/php-allowed-memory-size-exchausted-fatal-error/

Error Reporting http://php.net/manual/en/function.error-reporting.php

If you do not need i to go that high (9999999999) seems really high. Reduce the size.

Another issue you may be having is

Fatal error:  Maximum execution time of 60 seconds exceeded

You can always increase the execution time if you have to.

Max execution time http://php.net/manual/en/function.set-time-limit.php

also Increase max execution time for php

Really all this depends on what error you are getting and by starting with showing the error reporting, this will help determine what the real issue at hand is.

Upvotes: 4

Related Questions