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:
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 Here dividing by zero will throw an arithmetic exception which is a runtime exception |
For instance: <?php Here, due to missing semicolon leads to a syntax error which falls under compile time exception |