Skip to content

Instantly share code, notes, and snippets.

function bytes_to_human($bytes) {
$human = NULL;
if ($bytes < 1024) {
$human = number_format ($bytes, 0) .' bytes';
} else if ($bytes < 1024*1024) {
$human = number_format (($bytes / 1024), 1) . ' KB';
} else {
$human = number_format (($bytes / (1024*1024)), 1) . ' MB';
}
return $human;
<?php
$type = error_reporting();
$levels = array (
'E_ERROR' => (($type & E_ERROR) == E_ERROR),
'E_WARNING' => (($type & E_WARNING) == E_WARNING),
'E_PARSE' => (($type & E_PARSE) == E_PARSE),
'E_NOTICE' => (($type & E_NOTICE) == E_NOTICE),
'E_CORE_ERROR' => (($type & E_CORE_ERROR) == E_CORE_ERROR),
'E_CORE_WARNING' => (($type & E_CORE_WARNING) == E_CORE_WARNING),
@AllenJB
AllenJB / chart.time-preparse.js
Created May 13, 2016 16:14
Chart.js: Pre-parse time values into moment objects
var preparsePlugin = Chart.PluginBase.extend({
beforeInit: function(chartCtrlr) {
var chart = chartCtrlr.chart;
var labels = chartCtrlr.data.labels;
var momentLabels = [];
labels.forEach(function(v) {
momentLabels.push(moment(v, moment.ISO_8601));
});
chartCtrlr.data.labels = momentLabels;
},
<FilesMatch "^php-fpm-ping|php-fpm-status$">
SetHandler "proxy:unix:/var/run/php-fpm/dcr_v4.sock|fcgi://localhost/"
deny from all
allow from localhost
</FilesMatch>
@AllenJB
AllenJB / _solution notes
Last active September 12, 2016 14:01
Fun with perl system()
https://bugzilla.yoctoproject.org/show_bug.cgi?id=9131
Run: LD_USE_LOAD_BIAS=0 perl -e 'print fork'
Temporarily disables prelink
Permanent solution: unprelink all the things!
See also:
https://bugs.gentoo.org/show_bug.cgi?id=579374
@AllenJB
AllenJB / gist:872553b5cb2086b0b8de9b0c50044ce5
Last active June 30, 2018 17:47
MySQL join same table on different fields
SELECT table1.employee_id_1, table1.employee_id_2, e1.first_name AS e1_first_name, e2.first_name AS e2_first_name
FROM table1
LEFT JOIN employee_tbl AS e1 ON table1.employee_id_1 = e1.employee_id
LEFT JOIN employee_tbl AS e2 ON table2.employee_id_2 = e2.employee_id
@AllenJB
AllenJB / async-worker-cancel.php
Last active July 6, 2018 11:55
Bunny Async Worker w/ signal handler
<?php
use Bunny\Channel;
use Bunny\Async\Client;
use Bunny\Message;
use Bunny\Protocol\MethodBasicConsumeOkFrame;
use React\EventLoop\Factory;
require '../../vendor/autoload.php';
@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"];
@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 / _wishlist.md
Last active December 7, 2023 14:37
PHP Wishlist