What are the types of errors in PHP ?

  • In a programmer, error is a mistake that may be caused by writing incorrect code or incorrect syntax.
  • An error message is displayed on your browser containing the filename along with a message describing the error, location and the line number in which error has occurred.
  • In PHP there are various types of errors that contains basically four main types of errors, they are
    • Syntax Error or Parse Error
    • Fatal Error
    • Warning Error
    • Notice Error

Syntax Error or Parse Error

  • A syntax error is a mistake in the syntax of source code, which can be done by programmers due to their lack of knowledge or concern.
  • At compile time compiler is used to catch the syntax error.
  • Due to common reasons these errors can occur like missing semicolon, unclosed quotes, or unclosed brackets, extra or missing parentheses and many more.
  • While compiling the program syntax error can be caught by the compiler and gives a syntax error or parse error message.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%20%20%20%20%2F*——————syntax%20error——————-*%2F%20%20%0A%20%20%20%20echo%20%22Venkat%3A%20Hie!%20I’m%20Venkat.%20%3C%2Fbr%3E%22%3B%20%20%0A%20%20%20%20echo%20%22Lokesh%3A%20I’m%20Lokesh.%20How%20are%20you%3F%22%20%20%0A%20%20%20%20echo%20%22Venkat%3A%20I’m%20good!%20and%20you%3F%22%3B%20%20%0A%20%20%20%20echo%20%22Lokesh%3A%20I’m%20also%20good%22%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Fatal Error

  • Fatal error is another type of error, which is occurred due to the use of undefined function.
  • The PHP compiler understands the PHP code and also recognizes the undefined function.
  • PHP compiler generates a fatal error, when a function is called without providing its definition.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%20%20%20%20%2F*——————fatal%20error——————-*%2F%20%20%0A%20%20%20%20function%20add(%24f1%2C%20%24f2)%20%7B%20%20%0A%20%20%20%20%20%20%20%20%24sum%20%3D%20%24f1%20%2B%20%24f2%3B%20%20%0A%20%20%20%20%20%20%20%20echo%20%22Addition%3A%22%20.%20%24sum%3B%20%20%0A%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%24f1%20%3D%2023%3B%20%20%0A%20%20%20%20%24f2%20%3D%2056%3B%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2Fcall%20the%20function%20that%20is%20not%20defined%20%20%0A%20%20%20%20%2F%2FGenerate%20fatal%20error%20%20%0A%20%20%20%20catch_fatal_error()%3B%20%20%0A%20%20%20%20%2F%2Fecho%20%22Fatal%20Error%22%3B%20%20%20%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Warning Error

  • When the programmer tries to include a missing file there warning error is generated.
  • The PHP function calls that missing file which does not exist and warning error does not stop/prevent the execution of the program.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%20%20%20%20%2F*——————-warning%20error——————*%2F%20%20%0A%20%20%20%20%24cmpny%20%3D%20’Wikitechy’%3B%20%20%0A%20%20%20%20echo%20%22Warning%20Error%3A%20%22%3B%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2Finclude%20a%20file%20in%20the%20code%20%20%0A%20%20%20%20include%20(‘jtp.php’)%3B%20%20%20%20%20%20%0A%3F%3E%0A” message=”” highlight=”” provider=”manual”/]

Output

Notice Error

  • Notice error is also same as warning error, when program contains something wrong, the notice error occurs.
  • Including a notice error, it allows/continue the execution of the program.
  • Notice error does not prevent the execution of the code and generally it occurs when we try to use or access a variable which is undefined.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%20%20%20%20%2F*——————notice%20error——————-*%2F%20%20%0A%20%20%20%20%24telecom%20%3D%20%22Airtel%22%3B%20%20%0A%20%20%20%20echo%20%24telecom%3B%20%20%0A%20%20%20%20echo%20%24automobile%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like