📖 Scenario: You are building a simple PHP script to learn about different types of errors and how PHP reports them. This will help you understand how to spot and fix problems in your PHP code.
🎯 Goal: Create a PHP script that defines error reporting levels, triggers different types of errors, and displays the error messages.
📋 What You'll Learn
Create variables and settings to control error reporting
Trigger different PHP error types: notice, warning, and fatal error
Display error messages according to the error reporting level
💡 Why This Matters
🌍 Real World
Understanding PHP error types helps you debug your web applications and fix problems quickly.
💼 Career
PHP developers must know how to handle errors to write reliable and maintainable code.
Progress0 / 4 steps
1
Set up error reporting levels
Write PHP code to set the error reporting level to show all errors using error_reporting(E_ALL); and enable displaying errors with ini_set('display_errors', '1');.
PHP
Hint
Use error_reporting(E_ALL); to report all errors and ini_set('display_errors', '1'); to show them on the screen.
2
Trigger a notice error
Add PHP code that tries to use an undefined variable called $undefinedVar to trigger a notice error.
PHP
Hint
Simply try to print $undefinedVar without defining it first.
3
Trigger a warning error
Add PHP code to include a file named missingfile.php using include to trigger a warning error.
PHP
Hint
Use include 'missingfile.php'; to cause a warning because the file does not exist.
4
Trigger a fatal error
Add PHP code to call a function named undefinedFunction() that does not exist to trigger a fatal error. Then print "End of script" using echo.
PHP
Hint
Call undefinedFunction(); to cause a fatal error. The last echo will not run because of the fatal error.
Practice
(1/5)
1. Which PHP error type indicates a problem that stops the script from running?
easy
A. Warning
B. Fatal error
C. Notice
D. Deprecated
Solution
Step 1: Understand PHP error types
Fatal errors are serious problems that stop the script immediately.
Step 2: Compare with other error types
Warnings and notices do not stop the script; deprecated warns about old code.
Final Answer:
Fatal error -> Option B
Quick Check:
Fatal error = stops script [OK]
Hint: Fatal errors stop scripts immediately, others do not [OK]
Common Mistakes:
Confusing warnings with fatal errors
Thinking notices stop script execution
Mixing deprecated warnings with fatal errors
2. Which of the following is the correct way to set PHP to report all errors except notices?
easy
A. error_reporting(E_ALL & ~E_NOTICE);
B. error_reporting(E_ALL | E_NOTICE);
C. error_reporting(E_ALL & E_NOTICE);
D. error_reporting(E_ALL ^ E_NOTICE);
Solution
Step 1: Understand error_reporting flags
E_ALL means all errors; ~E_NOTICE means exclude notices using bitwise NOT.
Step 2: Analyze each option
error_reporting(E_ALL & ~E_NOTICE); uses bitwise AND with NOT (~) to exclude notices correctly.
Final Answer:
error_reporting(E_ALL & ~E_NOTICE); -> Option A
Quick Check:
Exclude notices with & ~E_NOTICE [OK]
Hint: Use & ~E_NOTICE to exclude notices from all errors [OK]
5. You want to show all errors except deprecated warnings during development but hide all errors on the live site. Which code setup achieves this best?
hard
A. error_reporting(E_ALL); ini_set('display_errors', 1);
B. error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', 0);
C. if (ENV == 'dev') { error_reporting(E_ALL & ~E_DEPRECATED); ini_set('display_errors', 1); } else { error_reporting(0); ini_set('display_errors', 0); }
D. if (ENV == 'prod') { error_reporting(E_ALL); ini_set('display_errors', 1); } else { error_reporting(0); ini_set('display_errors', 0); }
During development (dev), show all errors except deprecated; on live (prod), hide all errors.
Step 2: Analyze each option
if (ENV == 'dev') { error_reporting(E_ALL & ~E_DEPRECATED); ini_set('display_errors', 1); } else { error_reporting(0); ini_set('display_errors', 0); } correctly uses error_reporting with exclusion and display_errors on dev, off on live.
Final Answer:
if (ENV == 'dev') { error_reporting(E_ALL & ~E_DEPRECATED); ini_set('display_errors', 1); } else { error_reporting(0); ini_set('display_errors', 0); } -> Option C
Quick Check:
Dev shows errors except deprecated; live hides all [OK]
Hint: Use environment check to toggle error reporting and display [OK]
Common Mistakes:
Showing errors on live site
Not excluding deprecated warnings during dev
Confusing environment variable names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.