B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP PHP Logging and Debugging | LiveCodeProgramming

PHP Logging and Debugging

PHP provides several tools for logging and debugging: error_log(), var_dump(), print_r(), and custom try-catch error handling.

PHP में लॉगिंग और डिबगिंग के लिए error_log(), var_dump(), print_r() और try-catch जैसी सुविधाएँ मिलती हैं।

Example: error_log()

<?php
$name = "";
if ($name == "") {
  error_log("Name is empty!", 3, "logs/errors.log");
  echo "Error logged.";
}
?>
Error logged. (Check logs/errors.log)

Debug Output using var_dump()

<?php
$data = ["name" => "John", "age" => 30];
var_dump($data);
?>
array(2) { ["name"]=> string(4) "John" ["age"]=> int(30) }

Try-Catch Logging

<?php
try {
  throw new Exception("Something broke!");
} catch (Exception $e) {
  error_log($e->getMessage(), 3, "logs/exception.log");
  echo "Exception handled.";
}
?>
Exception handled. (Logged in exception.log)