1. What are PHP Errors? Reasons for Occurrence and 4 Main Types (Levels)

When learning programming, you may feel anxious when an English message appears on the screen or the screen goes blank. These are “errors,” but errors are actually “helpful messages that tell you why the program is not working correctly.”

First, let’s understand the main reasons why errors occur and the four major error levels (severity) in PHP.

Main Reasons Why Errors Occur

The causes of errors can be broadly classified into the following two categories.

  • Grammar mistakes (Syntax errors): A violation of writing rules. It is like a grammatical error in language, where the computer cannot understand the command.
  • Runtime inconsistency: Even if the grammar is correct, an impossibility arose during processing (e.g., trying to open a file that does not exist, or trying to calculate something incalculable).

4 Main Error Levels in PHP

PHP errors have “levels (severity),” which determine whether the program stops or not. Beginners should first learn the following four.

Error TypeEnglish NameSeverityStatusCharacteristics
Parse ErrorParse errorHighStopGrammar mistake. The program does not start at all.
Fatal ErrorFatal errorHighStopFatal mistake. Cannot proceed any further during execution.
WarningWarningMediumContinueWarning. There is something strange, but processing does not stop.
NoticeNoticeLowContinueNotice. Minor mistake or deprecated coding style.

2. [Parse error] Causes and How to Find Syntax Errors: Forgetting Semicolons or Brackets

Parse error is the most common error encountered by PHP beginners. Also known as “Syntax error,” the program stops without executing a single line because the writing style (grammar) itself is incorrect.

Common Causes

The most common cause is a simple typo.

  • Forgetting a semicolon (;): Forgetting to close the end of a line.
  • Forgetting to close brackets: The number of () or {} does not match.
  • Mixing in full-width spaces: Invisible full-width spaces are included in the code.

Concrete and Practical Code Examples

Writing code like the following will cause a Parse error.

<?php
// Example of forgetting a semicolon
$message = "Hello" 
echo $message;
?>

Error message example:

Parse error: syntax error, unexpected token “echo” in /path/to/file.php on line 4

How to Find and Fix

The error message displays the “line number where the error occurred (on line X).” However, in the case of a Parse error, the cause is very often on “the line before the pointed out line.”

In the example above, the error appears at echo on line 4, but the cause is the missing semicolon at the end of line 3.

Point: If a syntax error occurs, be sure to check not only the pointed out line but also the end of the line immediately before it.

3. [Fatal error] How to Deal with Fatal Errors: Mistakes in Functions or Files

Fatal error means a “deadly error.” It occurs when the grammar is correct but the content attempted to be executed is physically impossible, and the program terminates forcibly at that point.

Common Causes

  • Calling an undefined function: Trying to use a function that is not defined (or misspelled).
  • File loading failure: The file specified by require does not exist.
  • Out of memory: The processing was too heavy and exceeded the server’s memory limit.

Concrete and Practical Code Examples

Mistaking the function name (Typo) is a very common case.

<?php
function sayHello() {
    echo "Hello!";
}

// Mistaking the function name (sayHello -> sayHelo)
sayHelo(); 
?>

Error message example:

Fatal error: Uncaught Error: Call to undefined function sayHelo() in …

How to Fix

If “Undefined function” is displayed, first check the spelling of the function name. Also, if you are loading an external file, check if the file path (file location) is correct.

4. What is the Difference Between Warning and Notice? Points to Note for Errors That Do Not Stop the Program

Warning and Notice are characterized by the fact that even if an error occurs, the program processing itself does not stop and runs to the end. However, ignoring them can cause unexpected bugs and security holes.

What is a Warning?

It is a state where “processing can continue, but there is clearly an abnormality.”
For example, it occurs when trying to load a file with include and the file is not found. The process proceeds even without the file, but since it runs with missing necessary parts, it causes the website display to break.

<?php
// If the file does not exist, a Warning appears but processing proceeds
include 'not_exist_file.php'; 
echo 'This text will be displayed';
?>

What is a Notice?

It is a state where “there is no major impact on execution, but the writing style is incorrect.”
It is often seen when using a variable suddenly without defining it.

<?php
// Trying to display undefined variable $user
echo $user; 
?>

Error message example:

Notice: Undefined variable: user in …

Important Point: Why You Should Not Ignore Them

Since these do not stop the program, they tend to be ignored as “it’s working so it’s fine.” However, if the server settings change after public release, there is a risk that error messages will be displayed on the screen and seen by users. It is a professional rule to resolve (fix) everything during the development stage.

5. Basic Reading of PHP Error Messages and Deciphering Tips

Error messages may seem difficult because they are written in English, but they are actually written in a fixed format. If you can read the following three elements, you will find a clue to the solution immediately.

Anatomy of an Error Message

Let’s dissect a typical error message.

Parse error: syntax error, unexpected token "echo" in C:\xampp\htdocs\index.php on line 5
  • Error Type (What happened)
    Parse error: syntax error → “Grammar mistake”
  • Error Details (Hint for the cause)
    unexpected token "echo" → “There was an unexpected echo (high possibility that something is missing right before)”
  • Location of Occurrence (Where it happened)
    in ...\index.php on line 5 → “Discovered on line 5 of the file named index.php”

Tips for Efficient Deciphering

  • Check the line number: First look at on line XX and jump to that line.
  • Pick up words:
    • Undefined (Undefined …)
    • unexpected (Unexpected …)
    • No such file (No such file exists)
    You can guess the approximate cause just by these keywords.
  • Search: If you copy the message as is and search on Google, you will find solutions from people who encountered the same phenomenon (when doing so, delete your file path part before searching).

6. What if Errors are Not Displayed? php.ini Settings and WordPress Debug Mode

During development, a phenomenon where “the screen remains blank and nothing is displayed” may occur. This is because it is set to “not display errors on the screen.”

php.ini Settings (Local Development Environment, etc.)

You can control error display by editing the PHP configuration file, php.ini. Enable the following settings during development.

display_errors = On

Whether to output errors to the browser screen. It is basic to set it to On during development and Off during public release.

error_reporting = E_ALL

Up to what level of errors to report. If set to E_ALL, it will display everything including minor Notice levels.

WordPress Debug Mode

When using WordPress, it is common to edit the WordPress configuration file (wp-config.php) instead of php.ini.

Example setting for wp-config.php:

// Enable debug mode (errors will be displayed)
define( 'WP_DEBUG', true );

// Save errors to debug.log file without displaying on screen
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

This makes errors appear on the screen, allowing you to identify the cause.

7. Debugging Techniques and Recommended Tools for Efficient Bug Fixing

Finally, here are some techniques and tools to streamline error fixing (debugging).

Basic Debugging Technique: Check the Content of Variables

Many errors are caused by “variables not containing the expected data.” To confirm this, use the following functions.

  • var_dump($variable_name):
    Displays the contents of a variable (type, value, character count, etc.) in detail. Ideal for checking the contents of arrays and objects.
  • print_r($variable_name):
    A slightly simpler display than var_dump. Useful when you want to take a quick look at the structure of an array.
$fruits = array("apple", "orange");

echo "<pre>"; // Makes it easier to read with line breaks
var_dump($fruits);
echo "</pre>";

Recommended Tools (IDE Extensions, etc.)

By adding functions to your text editor, you can prevent errors before they happen.

  • VS Code (Visual Studio Code)
    The most standard editor in current Web development.
  • PHP Intelephense (VS Code Extension)
    While writing code, it tells you with a red wavy line that “a semicolon is missing” or “this variable is not defined.” Just installing this can prevent 80% of syntax errors.
  • Xdebug
    Although for intermediate users, it is a powerful tool that allows you to pause the program and check the state of variables at that moment.

Summary

Errors are not “failures,” but “guideposts” for completing the program. Once you acquire the skill to decipher error messages, your Web development speed will improve dramatically. First, start by trying to read the displayed English without fear.