Skip to content

Instantly share code, notes, and snippets.

@AllenJB
AllenJB / php-docs-phpstorm.md
Created October 11, 2025 18:41
PHP Docs + PHPStorm
@AllenJB
AllenJB / avoiding-sql-accidents.md
Last active October 7, 2025 12:49
Avoiding SQL Accidents in Production
  • Set up a "read-only" user in addition to your administrative user and use that by default unless you actually need to make changes on production servers.

  • Disable autocommit, or use explicit transactions so you can rollback mistakes. (Beware of implicit commits)

  • To avoid UPDATE without WHERE you can enable sql_safe_updates. Some clients have similar options.

  • Get into the habit of running a SELECT query before running an UPDATE to check which (and how many) records you've selected.

  • Use color schemes to differentiate between hosts. Many terminal clients and SQL clients have the ability to set / change the color scheme.

@AllenJB
AllenJB / jenkins.md
Last active December 21, 2022 11:31
Jenkins & PHP

Jenkins & PHP

This is a guide to setting up Jenkins for PHP projects. The Jenkins plugins required are linked in each section below.

Be sure to check the pipeline step reference link on the plugin pages linked below for full documentation and options for Jenkinsfile.

This guide uses Jenkinsfile based pipelines and assumes you already have a basic Jenkinsfile.

This guide assumes that you already have each of the tools mentioned configured for use outside of Jenkins. Only the necessary configuration options for working with Jenkins are mentioned.

@AllenJB
AllenJB / oom_example.php
Created December 3, 2022 10:35
PHP: Change memory_limit in shutdown handler after OOM
<?php
ini_set('memory_limit', '2M');
function shutdownHandler()
{
print "Shutdown handler triggered\n";
print "Memory limit: ". ini_get('memory_limit') ."\n";
// If the next line is commented, a second OOM will be triggered
ini_set('memory_limit', '-1');
print "Memory limit: ". ini_get('memory_limit') ."\n";
@AllenJB
AllenJB / DateTimeFactory.php
Created September 23, 2022 18:08
PHP DateTimeFactory
<?php
class DateTimeFactory
{
public static function createImmutableFromFormat(string $format, string $time, \DateTimeZone $timezone = null) : \DateTimeImmutable
{
$dt = \DateTimeImmutable::createFromFormat($format, $time, $timezone);
// DateTime errors/warnings can occur even if the object was successfully created (eg. invalid date)
@AllenJB
AllenJB / stylus.css
Last active August 1, 2020 13:44
June 2020 GitHub Redesign Modifications
.IssueLabel {
border-radius: 3px !important;
padding: 0 3px !important;
}
.UnderlineNav {
justify-content: center;
}
.UnderlineNav-item {
@AllenJB
AllenJB / gist:fd2fdea2ff3cbab7cbf74463f0650994
Created February 14, 2020 08:55
PHP Intentional Error Tests
public function fatalMemory() : void
{
$a = '';
while (true) {
$a .= str_repeat("Hello", 1024 * 1024);
}
}
public function timeout() : void
@AllenJB
AllenJB / _wishlist.md
Last active December 7, 2023 14:37
PHP Wishlist
@AllenJB
AllenJB / phpfpm-prometheus.php
Created July 12, 2019 19:50
PHP-FPM Pool Monitoring w/ Prometheus
<?php
declare(strict_types=1);
// Prometheus metrics exporter for PHP-FPM
header("Content-Type: text/plain; version=0.0.4");
$stats = null;
$protocol = ((!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") ? "https" : "http");
$result = file_get_contents($protocol ."://" . $_SERVER["SERVER_NAME"] . "/php-fpm-status?json&full");
if ($result !== false) {
@AllenJB
AllenJB / array_merge vs plus.php
Created March 25, 2019 09:07
PHP Canonical Answers & Stuff
<?php
// https://3v4l.org/e79nG
$arr1 = [1, 2, 3];
$arr2 = [2, 4, 1];
$arr1 += $arr2;
var_dump($arr1);
$arr1 = ["foo", "bar", "baz"];