📲 Download Utility Tools Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

15. Error Handling (PHP)

Error handling in PHP helps you detect, manage, and debug issues in your code efficiently.


🔴 1. Error Types in PHP

PHP has different types of errors:

✅ Common Error Types:

Error TypeDescription
E_NOTICEMinor issues (e.g., undefined variable)
E_WARNINGNon-fatal errors (script continues)
E_ERRORFatal error (script stops)
E_PARSESyntax errors
E_DEPRECATEDUsing outdated features
E_ALLAll errors

📌 Example:

echo $name; // Undefined variable → Notice

🟡 2. Displaying Errors

Enable error reporting:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Disable in production:

ini_set('display_errors', 0);

🟢 3. try-catch Blocks (Exception Handling)

Used to handle runtime errors (exceptions).

📌 Basic Example:

try {
$num = 10 / 0;
} catch (Throwable $e) {
echo "Error: " . $e->getMessage();
}

📌 Custom Exception:

function checkAge($age) {
if ($age < 18) {
throw new Exception("Underage!");
}
return "Access granted";
}try {
echo checkAge(16);
} catch (Exception $e) {
echo $e->getMessage();
}

🔵 4. Custom Error Handling

You can create your own error handler.

📌 Example:

function customError($errno, $errstr) {
echo "Error [$errno]: $errstr";
}set_error_handler("customError");// Trigger error
echo $test; // undefined variable

🟣 5. Trigger Custom Errors

trigger_error("Something went wrong!", E_USER_WARNING);

🟠 6. Debugging Techniques

✅ 1. Print Variables

print_r($data);
var_dump($data);

✅ 2. Use die() / exit()

die("Stop here");

✅ 3. Logging Errors

error_log("This is an error message", 3, "error.log");

✅ 4. Stack Trace

try {
throw new Exception("Test error");
} catch (Exception $e) {
echo $e->getTraceAsString();
}

🔥 7. Best Practices (Important)

  • ❌ Don’t show errors to users in production
  • ✅ Log errors instead
  • ✅ Use try-catch for critical operations
  • ✅ Validate inputs properly
  • ✅ Use custom error handlers for large apps

🚀 Real-World Example

try {
$conn = new mysqli("localhost", "root", "", "test"); if ($conn->connect_error) {
throw new Exception("Database connection failed");
} echo "Connected successfully";} catch (Exception $e) {
error_log($e->getMessage());
echo "Something went wrong. Please try later.";
}


AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes