Skip to content

Instantly share code, notes, and snippets.

@nd3w
Last active January 22, 2026 00:53
Show Gist options
  • Select an option

  • Save nd3w/743a04fd3add430c19237c393a5103a9 to your computer and use it in GitHub Desktop.

Select an option

Save nd3w/743a04fd3add430c19237c393a5103a9 to your computer and use it in GitHub Desktop.
PHP Snippets

Dump all PHP variables

$all_vars = get_defined_vars();
print_r($all_vars);

Create an array for a range (e.g: array with number from 1 to 9)

$number = 9;

for ($i = 1; $i <= $number; ++$i) {
    $array[] = $i;
}

Number to words in Bahasa Indonesia (intl extension needed)

$numberFormatter = new \NumberFormatter("id_ID", \NumberFormatter::SPELLOUT);
echo $numberFormatter->format('1075000');
// Print: satu juta tujuh puluh lima ribu

Redirect any URL without http to https:

if (defined('APP_ENV') && APP_ENV === 'production') {
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
        || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
        || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
        || (!empty($_SERVER['HTTP_CF_VISITOR']) && str_contains($_SERVER['HTTP_CF_VISITOR'], '"https"'))) {
        return;
    }

    $host = $_SERVER['HTTP_HOST'] ?? '';
    $uri  = $_SERVER['REQUEST_URI'] ?? '/';

    header('Location: https://' . $host . $uri, true, 301);
    exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment