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 / 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 / PHP: array_insert.php
Last active February 10, 2017 21:52
PHP: array_insert() to insert at index, non-destructively
<?php
/**
* Insert a value at a specified integer index non-destructively with minimal
* impact on indexing. Indexes below the insert location are not changed and
* indexes above the insertion are shift up until the point where a free slot is
* found, above which indexing is unaltered.
*
* As for sorting, integer indexes will sorted up to this free slot but not above.
* Every element above this location, whether the key is an integer or string,
* is appended to the end of the array unsorted. If the 4th argument is true the
@Jeff-Russ
Jeff-Russ / htaccess.sh
Last active January 29, 2017 07:06
.htaccess file generators
#!/bin/bash
# DIRECTIONS: run these:
# $ chmod u+x ./htaccess.sh
# $ source ./htaccess.sh
# THEN navigate anywhere and run the functions as
# if they are built in commands. See below for more.
#################################################################
@Jeff-Russ
Jeff-Russ / Bash: Pretty Prompt.md
Last active February 3, 2017 21:55
Bash: Pretty Prompt

Bash: Pretty Prompt

place this in ~/.bashrc or ~/.bash_profile

_pretty_prompt() {
	# args: $maincolor_int, $dircolor_int, $gitcolor_int, $style_str 
	# color ints: green 32, yellow 33, cyan 36, white 37, reset(none) 00
	# style: '1;2;4;' means bold dim and underline. leave off for no style
@Jeff-Russ
Jeff-Russ / PHP: array to string pretty json or php format.php
Created January 22, 2017 05:58
PHP: array to string pretty json or php format
<?php
function array_string($arr,$opts='php') {
if (is_array($opts)) $opts['depth']++;
else {
if (!is_string($opts)) {
$in = $opts;
$opts = is_integer($in) ? "json" : 'php';
if ($in) $opts .= " pretty print";
@Jeff-Russ
Jeff-Russ / phpDoc Usage Guide.md
Last active May 31, 2017 16:14
phpDocumentor Usage Guide

PHPDoc Usage Guide

Basic Info

@version [version] Provides the version number of a class or method
@author Author Name <author@email> The author of the current element
@copyright name date Documents copyright information
@package name of a package A group of related classes and functions
@subpackage sub-package under @package classes or functions
@todo Documents things that need to be done to the code at a later date

@Jeff-Russ
Jeff-Russ / PHP: extends Exception.md
Last active January 22, 2017 00:24
PHP: extends Exception
<?php
/**
 * error-and-status/Exc.php Defined Exc class
 * 
 * @package    error-and-status
 * @author     Jeff Russ
 * @license    GPL-2.0
 */
@Jeff-Russ
Jeff-Russ / PHP: is_ref_to.md
Last active January 18, 2017 22:48
PHP: Test if Variable is Reference to Another

PHP Testing if Two Variables are Referring to Same Data.

Adapted from this

PHP's strictest comparison operators still only go as far as indicating whether two variables have the same value but what if you want to be sure one variable is a reference to another, not just a container for a matching value?

PHP has no built in way to do this as of now but you can work around this by doing a test of your own. If you have this:

<?php
class Dynamic {
public static $static_methods = array();
// we can't have a $static_props because there is no __getStatic or __setStatic !!
# $prop_defaults adds a property for all object (key) and provide a default value (value)
# and is only in validating a new property on the object and not for storage.
public static $prop_defaults = array();
public $method = array();
@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;