Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
Difference between Interrupt and Exception
Next article icon

Difference between runtime exception and compile time exception in PHP

Last Updated : 01 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
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


Next Article
Difference between Interrupt and Exception
author
rijushree100guha
Improve
Article Tags :
  • Difference Between
  • Web Technologies
  • PHP
  • PHP-Questions
  • Web Technologies - Difference Between

Similar Reads

  • Difference between Compile Time Errors and Runtime Errors
    Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by the compiler and thus are known as compile-time errors. M
    3 min read
  • Difference between Exception::getMessage and Exception::getLine
    Exception::getMessage: The getMessage exception in PHP language is basically used by the programmers to know the Exception message. It means that whenever an exception condition occurs in a code, in order to know the exact meaning and what that exception is all about this function is been used. This
    2 min read
  • Difference between die() and exit() functions in PHP
    PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
    2 min read
  • Difference between Interrupt and Exception
    In computer systems, interrupts and exceptions are like signals that tell the CPU to pause what it’s doing and handle something important. They both temporarily stop a running program, but they are not the same. They come from different sources and are used for different reasons. Knowing the differe
    3 min read
  • What is the difference between fopen() and fclose() functions in PHP ?
    In this article, we are going to discuss the differences between fopen() and fclose() functions. fopen() function: This function helps users to open a file or a web URL. It mainly accepts two parameters - One is $file that specifies the file or an URL to be opened and the other is $mode that specifi
    2 min read
  • Difference between require() and require_once() in PHP
    PHP require() Function: The require() function in PHP is mostly used to include the code/data of one PHP file to another file. During this process, if there are any kind of errors then this require() function will display a warning along with a fatal error which will immediately stop the execution o
    3 min read
  • Explain the Difference Between shell_exec() and exec() Functions
    In this article, we will learn about the shell_exec() & exec() functions in PHP. As we know that in order to execute a command in a system, we need the shell of the respective operating systems, but if we need to execute the same command with the help of a programming language like PHP, then we
    4 min read
  • Difference between Compiled and Interpreted Language
    Prerequisite - Compiler vs Interpreter What is Compiled Language? A compiled language is a programming language that is generally compiled and not interpreted. It is one where the program, once compiled, is expressed in the instructions of the target machine; this machine code is undecipherable by h
    2 min read
  • Difference Between except: and except Exception as e
    In Python, exception handling is a vital feature that helps manage errors and maintain code stability. Understanding the nuances of different exception-handling constructs is crucial for writing robust and maintainable code. This article will explore the difference between except: and except Excepti
    5 min read
  • What is the difference between == and === in PHP ?
    In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences