This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext = null) { | |
prs("here! $errstr"); | |
}); | |
class ErrorHelper { | |
var $lastError; | |
function setErrorHandler() { | |
$this->lastError = null; | |
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext = null) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function super(...$args) { | |
$object = debug_backtrace(1, 2)[1]['object']; | |
$parent = get_parent_class($object); // Let it errorize. | |
if ($parent && method_exists($parent, '__construct')) { | |
$ref = new ReflectionMethod($parent, '__construct'); | |
$ref->invoke($object, ...$args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function is_prime(int|float $n): bool { | |
if ($n == 2) | |
return true; | |
if ($n < 2 or $n % 2 == 0) | |
return false; | |
for ($i = 3; $i <= sqrt($n); $i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rewrite | |
- if ($shippingInfo && $shippingInfo['cost'] && $this->formatPrice($shippingInfo['cost'] != 0)) { | |
+ if (isset($shippingInfo['cost']) && $this->formatPrice($shippingInfo['cost']) > 0) | |
- $order_id = $this->request->get['order_id']; | |
+ $order_id = (int) $this->request->get['order_id']; | |
// Refactor | |
private function formatPrice($number) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="c" width="1024" height="1024"> | |
<script> | |
// Creds: https://twitter.com/aemkei/status/1378106731386040322 | |
// let cx = c.getContext('2d'), x, y | |
// for (x = 0; x < 256; x++) { | |
// for (y = 0; y < 256; y++) { | |
// if ((x ^ y) % 2) { | |
// cx.fillRect(x * x, y * y, x, y); | |
// } | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://github.com/itchyny/gojq#difference-to-jq | |
$intdiv = fn($x, $y) => ($x - $x % $y) / $y; | |
// polyfill | |
if (!function_exists('intdiv')) { | |
function intdiv($x, $y) { | |
return ($x - $x % $y) / $y; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://betterexplained.com/examples/birthday/birthday.html | |
function calc(people) { | |
let ret = { | |
days: 365, | |
people: people | |
} | |
ret.combinations = ret.people * (ret.people - 1) / 2 | |
ret.chance = (ret.days - 1) / ret.days | |
ret.expected = 1 - (ret.chance ** ret.combinations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('each')) { | |
function each(array &$array) { | |
$value = current($array); | |
$key = key($array); | |
if (is_null($key)) { | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert_base(input, fromBase, toBase) | |
{ | |
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
if (typeof fromBase == 'number') { | |
if (fromBase < 2 || fromBase > 62) { | |
throw ('Invalid base for from chars, min=2 & max=62') | |
} | |
fromBase = chars.substr(0, fromBase) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { protected int $x; } | |
class AA extends A { protected $x; } | |
// -> PHP Fatal error: Type of acme\AA::$ must be int (as in class acme\A) | |
No var name here "acme\AA::$". |
NewerOlder