Skip to content

Instantly share code, notes, and snippets.

<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);
// }
// }
// 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)
{
@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++) {
@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 / 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) {