1. <?php
  2. /**
  3. * Suppresses errors for specific text blocks during development.
  4. *
  5. * This function wraps code blocks with error suppression for debugging purposes.
  6. * It should be used cautiously and removed before deploying to production.
  7. *
  8. * @param string $startTag The opening tag for the error suppression block.
  9. * @param string $endTag The closing tag for the error suppression block.
  10. */
  11. function suppressErrors(string $startTag, string $endTag): void
  12. {
  13. error_reporting(E_ALL); // Report all errors initially
  14. ini_set('display_errors', 1); // Display errors
  15. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); // Suppress specific error types
  16. ob_start(); // Start output buffering
  17. $code = $_SERVER['REQUEST_URI']; // Get the current request URI
  18. if (strpos($code, $startTag) !== false) {
  19. // If the start tag is found, suppress errors
  20. error_reporting(0); // Turn off error reporting
  21. ini_set('display_errors', 0); // Don't display errors
  22. }
  23. ob_end_flush(); // Flush the output buffer
  24. }
  25. /**
  26. * Example usage:
  27. *
  28. * Wrap code blocks with the suppressErrors function to disable error reporting.
  29. * Example:
  30. * <suppressErrors><code class="php">
  31. * <?php
  32. * // Code that might generate errors
  33. * $variable = null;
  34. * echo $variable->someMethod();
  35. * ?>
  36. * </suppressErrors>
  37. */
  38. ?>

Add your comment