Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile
@Jeff-Russ
Jeff-Russ / Array Objects in PHP.md
Last active April 5, 2024 21:07
PHP: ArrayObject, IteratorAggregate , ArrayAccess , Serializable , Countable

Array Objects in PHP

Array are not object in PHP but PHP does give us some ways to make object that act like arrays. First, there is the ArrayObject class which is pretty close to what you have with a normal array (you can iterate it, use [] etc.) If you use it as a parent for your class you can add methods to it.

class ArrObj extends ArrayObject{
	// add methods here
@Jeff-Russ
Jeff-Russ / Demo: String Templates in Bash .md
Last active December 11, 2023 19:21
The best way to create a multiline document template (shell)

Demo: String Templates in Bash

The best way to create a multiline string template variable which has variables within it and contains double quotes.

IMPORTANT: The variable inserts must be assigned before the string template is declared and before it's used! EACH TIME THE INSERTS ARE MODIFIED, THE TEMPLATE MUST BE RE-RECLARED! For this reason it's sometimes handy to put the template def inside a function so you can call/re-call it before using it.

@Jeff-Russ
Jeff-Russ / Bit_Manipulation.h
Created October 22, 2018 07:18
C/C++ Macros for Bit Manipulation i.e. Arduino
/* Bit Manipulation Macros
A good article: http://www.coranac.com/documents/working-with-bits-and-bitfields/
x is a variable that will be modified.
y will not.
pos is a unsigned int (usually 0 through 7) representing a single bit position where the
right-most bit is bit 0. So 00000010 is pos 1 since the second bit is high.
bm (bit mask) is used to specify multiple bits by having each set ON.
bf (bit field) is similar (it is in fact used as a bit mask) but it is used to specify a
range of neighboring bit by having them set ON.
@Jeff-Russ
Jeff-Russ / PHP Performance: if,elseif vs case,switch vs nested if.php
Created December 17, 2016 03:00
PHP Performance: if/elseif vs case/switch vs nested if
<?php
$iter = 500000;
$start = microtime(true);
$n = 0;
for ($c=0; $c<7*$iter; $c++) {
$i = $c % 16;
if ($i===0) $n += 0;
@Jeff-Russ
Jeff-Russ / PHP: numeric array keys.md
Last active January 27, 2023 14:06
PHP: numeric array keys

PHP's Numeric & String Key Type Juggling

Arrays in PHP treat integer and string integers synonymously. If you set $a['1'] = '1' and access $a[1] you will get '1'. This is because php juggled your string key to an integer before assigning it; the assigment was actually made to an integer key. PHP also juggles in the process of accesssing too which means if you try to access [1] with ['1'] it will work. Therefore there is no real distinction between string integers and real integer, you can use them interchangably.

Remember that '1.0' does not equal '1' or 1 with strict comparison. This is true of array keys as well: you can have both ['1.0'] and ['1'] and they would be different elements but not [1] and ['1'].

@Jeff-Russ
Jeff-Russ / gitReview.sh
Last active December 16, 2022 19:41
gitReview allows navigation through commit histories and release tags, avoiding detached HEAD annoyances.
# Copyright (c) 2022, Jeff Russ
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# NOTE: This script requires zsh and peco (see https://github.com/peco/peco)
# It be sourced from ~/.zshrc and the gitReviewSetup function should be called
# once per terminal session where the $PWD is under git revision control.
@Jeff-Russ
Jeff-Russ / PHP: Sub-Array Functions.php
Last active July 17, 2022 04:42
PHP: Sub-Arrays: Rotating, Sorting, Categorizing, Flattening, and Merging
<?php namespace Jr;
##### GETTING FIRST OR LAST MATCH ONLY
# Find key of subarray that has a specified key with a specified value
function subarray_find ($arr, $subkey, $subval, $get_last=false) {
if ($get_last) $arr = array_reverse($arr, true);
foreach ($arr as $key=>$arr) {
foreach ($arr as $k=>$v) {
if ($k===$subkey && $v===$subval) return $key;
@Jeff-Russ
Jeff-Russ / Show Safari History with Timestamps.md
Last active June 18, 2020 21:01
Show recent Safari history with timestamps (command line function)

Show Recent Safari History with Timestamps

This creates a command for you to run in the terminal (or similar) zsh shell terminal app. I believe this should work in bash terminals as well. Once you're done setting it up you simply type

Jeff@MBP ~ % safariHist 8 
Thu Jun 18 16:50:59 EDT 2020 | YouTube
Thu Jun 18 16:50:51 EDT 2020 | Browse All of Google's Products & Services - Google
Thu Jun 18 16:50:49 EDT 2020 | Browse All of Google's Products & Services - Google
Thu Jun 18 16:50:47 EDT 2020 | Google - About Google, Our Culture &amp; Company News
@Jeff-Russ
Jeff-Russ / node-readline-sync.md
Last active May 14, 2019 11:55
A synchronous & blocking readline prompt solution for Node.js that's not a huge node package

node-readline-sync

a synchronous & blocking readline prompt solution for Node.js that's not a huge node package

Why ask the user for input if you're just going to go ahead and continue executing anyway? Usually when you prompt the user for a response, everything that transpires depends on the answer they give. When you ask the user "are you sure" before changing their legal name to "Diarrhea Jones", blocking is a GOOD THING.

Luckily there is a workaround that's so simple it doesn't actually need it's own git repository.

Installation Instructions

@Jeff-Russ
Jeff-Russ / arduinoVCC.md
Last active October 31, 2018 16:11
Accurate Arduino analogRead() with a Simple VCC Detection

Accurate Arduino analogRead() with a Simple VCC Detection

This is a function to determine your Arduino's supply voltage automatically in order to convert the results of analogRead into a much more accurate voltage. As you may know, analogRead() returns a value that's relative to the supply voltage to the Arduino, which can deviate. If you feed a known voltage to an analog pin, this function can read and use it to calculate what Arduino's supply voltage is:

double getArduinoVcc(int analog_pin, double expected_volt)
{

double received_frac = (double)analogRead(analog_pin) / 1023.0;