Open In App

Difference between runtime exception and compile time exception in PHP

Last Updated : 01 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension “.php”. It is an interpreted language and it does not require a compiler. PHP code is executed in the server.

An Exception is an object in PHP that defines a logical or runtime error or some unexpected behavior of a PHP file. It is the way to deal with that it cannot use while executing a PHP file. PHP classes, PHP functions, as well as user-defined functions, can throw exceptions. There are 2 types of exceptions in PHP: 

Runtime Exception: These exceptions are exceptions that can occur even when the complete code is logically and syntactically correct. These errors can occur from an invalid communication to an API, invalid input from the user, when storage place exceeds, etc. We don’t need to fix our code to remove a runtime exception if it occurs. If exceptions get uncaught it needs to be handled using a try-catch block as well as logging the error and reporting the error to the user to take further steps etc.

Example 1: This example describes the Runtime Exception in PHP.

PHP




<?php
   $x = 200;
   $y = 0;
   echo $x/$y;
?>


Output:

INF

Note: In PHP this output means infinity as dividing by zero is undefined.

Compile Time Exception: Compile time exceptions are exceptions in PHP that occur when the code is syntactically or semantically incorrect. These errors can be in the form of missing semicolons, undeclared variables, missing parenthesis, etc. Here we need to manually fix the code to remove the exception. Therefore it does not make sense to handle it using a try-catch block as the code should be fixed then and there.

Example 2: This example describes the Compile Time Exception in PHP.

PHP




<?php
  $x = 200;
  $y = 55
  echo $x+$y;
?>


Output:

Compile time exception as there is a missing semicolon in the third line

Difference between Runtime Exception and Compile Time Exception in PHP:

Runtime Exception

Compile Time Exception

These exceptions are exceptions that can occur even when the complete code is logically and syntactically correct.

Compile time exceptions are exceptions in PHP that occur when the code is syntactically or semantically incorrect.

These errors can occur from an invalid communication to an API, invalid input from the user, when storage place exceeds, etc. 

These errors can be in the form of missing semicolons, undeclared variables, missing parenthesis, etc.

We don’t need to fix our code to remove a runtime exception if it occurs. If exceptions get uncaught it needs to be handled using a try-catch block as well as logging the error and reporting the error to the user to take further steps etc.

Here we need to manually fix the code to remove the exception. Therefore it does not make sense to handle it using a try-catch block as the code should be fixed then and there.

For instance:

<?php
   $x = 200;
   $y = 0;
   echo $x/$y;
?>

Here dividing by zero will throw an arithmetic exception which is a runtime exception

For instance:

<?php
$x = 200;
$y = 55
echo $x+$y;
?>

Here, due to missing semicolon leads to a syntax error which falls under compile time exception



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads