Skip to content

Instantly share code, notes, and snippets.

@krmgns
krmgns / error.php
Created October 18, 2022 11:08
Error handling in PHP
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) {
@krmgns
krmgns / super.php
Created June 1, 2022 19:18
Fake super() function for PHP, just like parent::__construct().
<?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);
}
}
@krmgns
krmgns / is_prime.php
Created December 1, 2021 21:40
Prime number check.
<?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++) {
// 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)
{
<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);
// }
// }
@krmgns
krmgns / intdiv.php
Last active March 15, 2021 11:50
intdiv samples & polyfill.
<?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;
}
}
@krmgns
krmgns / birthday_paradox.js
Created March 4, 2021 11:35
Birthday paradox.
// 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)
@krmgns
krmgns / each.php
Last active October 28, 2022 09:47
Polyfill for "each" function that was removed as of PHP/8
<?php
if (!function_exists('each')) {
function each(array &$array) {
$value = current($array);
$key = key($array);
if (is_null($key)) {
return false;
}
@krmgns
krmgns / convert_base.js
Created December 13, 2020 04:10
convert_base.js
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)
@krmgns
krmgns / Uninitialized typed properties 4.bug
Created December 8, 2019 22:05
Another "uninitialized" thing..
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::$".