error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
// Common error constants:
// E_ALL - All errors and warnings
// E_ERROR - Fatal runtime errors
// E_WARNING - Runtime warnings
// E_PARSE - Compile-time parse errors
// E_NOTICE - Runtime notices
// E_DEPRECATED - Warnings about deprecated features
try {
// Your code here
throw new Exception('Error message');
} catch (Exception $e) {
error_log($e->getMessage());
// or
error_log(print_r($e, true));
}
var_dump($var) - Detailed infoprint_r($var) - Readable formatvar_export($var) - PHP code formatdebug_print_backtrace() - Call stackdebug_backtrace() - Stack trace arrayphpinfo() - PHP configurationget_defined_vars() - All variablesmemory_get_usage() - Memory usageerror_get_last() - Last error// Array manipulation
$array = array_map(fn($x) => $x * 2, $array);
$filtered = array_filter($array, fn($x) => $x > 0);
$sum = array_reduce($array, fn($a, $b) => $a + $b, 0);
// Array checking
in_array($needle, $haystack);
array_key_exists($key, $array);
isset($array[$key]);
// String manipulation
$str = trim($str);
$pos = strpos($haystack, $needle);
$sub = substr($string, $start, $length);
$replaced = str_replace($search, $replace, $subject);
// Regular expressions
preg_match($pattern, $subject, $matches);
preg_replace($pattern, $replacement, $subject);
// Current date/time
echo date('Y-m-d H:i:s'); // 2024-01-20 15:30:45
echo date('d/m/Y'); // 20/01/2024
echo date('l, F j, Y'); // Saturday, January 20, 2024
// Format with strftime
setlocale(LC_TIME, 'en_US');
echo strftime('%B %d, %Y'); // January 20, 2024
// DateTime class
$date = new DateTime();
echo $date->format('Y-m-d'); // 2024-01-20
Y - Full year (2024)y - Year, 2 digits (24)m - Month, with leading zeros (01-12)n - Month, without leading zeros (1-12)d - Day of month, with leading zeros (01-31)j - Day of month, without leading zeros (1-31)H - 24-hour format (00-23)h - 12-hour format (01-12)i - Minutes (00-59)s - Seconds (00-59)D - Short day name (Mon-Sun)l - Full day name (Monday-Sunday)M - Short month name (Jan-Dec)F - Full month name (January-December)// Add/subtract days
echo date('Y-m-d', strtotime('+1 day'));
echo date('Y-m-d', strtotime('-2 weeks'));
echo date('Y-m-d', strtotime('+3 months'));
// Using DateTime
$date = new DateTime();
$date->modify('+1 day');
$date->add(new DateInterval('P2D')); // Add 2 days
$date->sub(new DateInterval('P1M')); // Subtract 1 month
// Date difference
$date1 = new DateTime('2024-01-01');
$date2 = new DateTime('2024-02-15');
$interval = $date1->diff($date2);
echo $interval->days; // 45
// Set timezone
date_default_timezone_set('America/New_York');
// Get current timezone
echo date_default_timezone_get();
// Check valid date
var_dump(checkdate(2, 29, 2024)); // true (leap year)
var_dump(checkdate(2, 29, 2023)); // false
// Parse date string
$timestamp = strtotime('next Monday');
$timestamp = strtotime('2024-01-20 15:30:00');
// Compare dates
$date1 = strtotime('2024-01-20');
$date2 = strtotime('2024-02-15');
if ($date1 < $date2) {
echo 'Date1 is earlier';
}