Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active January 6, 2025 11:04
Show Gist options
  • Save vijinho/3c138a351401eaac9d603436d973d156 to your computer and use it in GitHub Desktop.
Save vijinho/3c138a351401eaac9d603436d973d156 to your computer and use it in GitHub Desktop.
output text if DEBUG or VERBOSE constant set
<?php
define('DEBUG', 1);
define('VERBOSE', 1);
// Output verbose message if VERBOSE constant is set
function verbose(string $message, mixed $data = ''): bool {
if (VERBOSE && !empty($message)) {
echo trim('[V ' . memoryUsage() . '] ' . $message) . "\n";
if (!empty($data)) {
print_r($data);
}
return true;
}
return false;
}
// Output debug information if DEBUG constant is set
function debug(string $message, mixed $data = ''): bool {
if (DEBUG) {
echo trim('[D ' . memoryUsage() . '] ' . $message) . "\n";
if (!empty($data)) {
print_r($data);
}
return true;
}
return false;
}
// Get memory usage in MB
function memoryUsage(): string {
$memory = memory_get_usage();
$peakMemory = memory_get_peak_usage();
return sprintf(
'%s/%s MB',
ceil($memory / 1024 / 1024),
ceil($peakMemory / 1024 / 1024)
);
}
// Example usage
verbose('Test Verbose', $_ENV ?? []);
debug('Test Debug', $_ENV ?? []);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment