What causes php fatal error?

Asked 7 years, 2 months ago

Viewed 7k times

I want script to stop being executed at the point of the crash, while not to exhaust the memory or otherwise crash the server.

Any idea how to do that?

asked Jul 8, 2015 at 5:56

4

Bit late but this is a guaranteed natural fatal error:

Call an undefined function, eg:

noSuchFunction[];

Or if you want this to be executed when you are in 'test' mode only, then put it into an eval

if[$isTestMode==1]{
  eval["noSuchFunction[];"];
}

Note: With the trigger_error[] function the problem is that if you are using set_error_handler[] it ceases to be fatal - your error handler will decide what to do with it.

answered Oct 19, 2016 at 5:09

Educative Answers Team

Fatal errors crash the program. There are three types of fatal errors in PHP:

  1. Startup fatal error: This error occurs when a system can’t run the code during installation.

  2. Compile time fatal error: This error occurs if a call is made to a non-existent code or variable.

  3. Runtime fatal error: This error occurs while the program is running, and will cause the program to quit.

The following code tried to call a function name without declaring the function. This gives a fatal error:

Notice how the print statement is not executed. This is because, as soon as a fatal error is thrown, the code stops compiling.

Solution

  • Look for the undeclared variables as given in the error.
  • If you are using inbuilt functions, ensure that there is no typo and the correct function is called.
  • Check if the spellings are correct.

Copyright ©2022 Educative, Inc. All rights reserved

1 Introduction: why does Fatal Error happen?

Out-of-memory errors are one of the most common and hard-to-fix problems that PHP developers run into — especially with applications that process large amounts of data — thanks to PHP's relatively conservative default memory settings. In fact, there are more than 1,300 questions related to PHP memory errors on Stack Overflow alone.

98% of the time this error comes from loading more into memory than what you set up PHP to handle in one process. There are other causes, but these are much less common — very rarely it can be a memory leak if you're on PHP 5.3 and above.

If you aren't sure what your PHP memory limit is set to, it's helpfully included in the error message. The size is reported in bytes, though, so we've done some conversions for you:

  • PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
  • PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
  • PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
  • PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
  • PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
  • PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
  • PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
  • PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB

2 What do I have to do to resolve it?

Your first course of action is to increase your memory limit. Note, this is a temporary debugging producedure. The goal is to increase the memory to a point where we have the application working again for the purpose of then reducing the memory usage. Once you decrease the memory usage you can lower the memory limit it to a value that's more suitable. Your plan should be to use as little memory as you could practically use where the application works and functions correctly in a production server based on the workload by your users [humans or programmatic]. I usually recommend setting the memory limit to something high, like 1GB, assuming you have at least 150% of that free in RAM.

Also, never do these tests on a production server unless you're sure you have plenty of RAM and you fully understand how web server processes consume memory. You could easily bring a server to its knees if there are many concurrent processes running, each using a high amount of memory. I would never, ever recommend setting the memory limit to -1 [unlimited] in a production environment. That's a recipe for disaster. Don't make that newbie mistake.

So how do you do this? Simple — increase the memory limit programmatically early on in your code, before your process runs out of memory. If you do it this way, you can give PHP extra memory only when that piece of code gets called rather than increasing the memory limit for all PHP processes.

Chủ Đề