Skip to content

Instantly share code, notes, and snippets.

@rotexdegba
rotexdegba / generate-backtrace-info-for-logging.php
Created Feb 7, 2014
This php code snippets generates a backtrace log string of method call stacks along a an inheritance hierarchy. Work in progress.
View generate-backtrace-info-for-logging.php
<?php
////////////////////////////////
/// Generate Call Trace Info ///
////////////////////////////////
$backtrace = debug_backtrace(~DEBUG_BACKTRACE_PROVIDE_OBJECT);
$backtrace_start_index = 1; //index to start reading from in the
//array returned by debug_backtrace
$class = "[\n";
@rotexdegba
rotexdegba / unique-indexes-mysql.md
Created Feb 11, 2014
How to Use Unique Indexes in MySQL and Other Databases - SitePoint
View unique-indexes-mysql.md

MySQL NULLs I say almost any database because MySQL has an odd quirk. NULL is treated as a unique value — which is why you cannot use comparisons such as value = NULL and need to use value IS NULL. Unfortunately, this also affects unique indexes and no logic has been implemented to fix it.

We can execute our original INSERT multiple times and a new record will be created each time because the extension field defaults to NULL and is considered to be unique:

INSERT INTO phone

(country, area, number)

(1, 234, 567890);

@rotexdegba
rotexdegba / php-password-hashing-demo.php
Last active Aug 29, 2015
PHP Password hashing demo.
View php-password-hashing-demo.php
<?php
function register($user, $password) {
$hash = password_hash($password, PASSWORD_BCRYPT);
$this->store($user, $hash);
}
function login($user, $password) {
$hash = $this->fetchHash($user);
@rotexdegba
rotexdegba / in-array-optimizations.php
Last active Aug 29, 2015
Alternative implementation of in_array using either isset or array_key_exists. This approach requires restructuring the original array.
View in-array-optimizations.php
<?php
//This isset optimization is not suitable for arrays that contains null values.
/*
$a = array('a' => null);
isset($a['a']); // Will be FALSE
array_key_exists('a', $a); // Will be TRUE
*/
$total = 10000;
$paragraph = 'this is a sentence. Crud! $$$$!';
@rotexdegba
rotexdegba / quickly-convert-xml-to-php-objects.php
Created Apr 9, 2015
How to Quickly Convert Xml Read into a SimpleXMLElement object into PHP Objects
View quickly-convert-xml-to-php-objects.php
<?php
$url = 'http://some-server.com/some-file.xml';
$simplexmlelement_obj = simplexml_load_file($url);
$array_of_objects_from_xml = array();
if ($simplexmlelement_obj !== false) {
$xml_as_json = json_encode($simplexmlelement_obj);
$array_of_objects_from_xml = json_decode($xml_as_json);
@rotexdegba
rotexdegba / mem-consumption-tests.php
Created May 26, 2015
PHP Script to Benchmark Memory Consumtion on Common Array / Object / String Memory Consumption
View mem-consumption-tests.php
<?php
ini_set('memory_limit', '1024M');
function test(){
$index = 0;
//Case 1
$a = '';
View Mint704_Assignment1.py
#A program that computes the maximum integer value before
#python switches to using the long data type for a value
#There are 2 methods used here, one fast and the other takes a while to compute
#Usage:
#python Mint704_Assignment1.py
import sys
def slow_solution():
@rotexdegba
rotexdegba / mysql-command-line-user-creation.sh
Last active Dec 31, 2015
MySQL commands to create users via the mysql command line utility
View mysql-command-line-user-creation.sh
# mysql --user=root --password=somepassword #connect to mysql as root or any other user that has the appropriate user creation permissions
# mysql> CREATE USER 'rotimi'@'localhost' IDENTIFIED BY 'password';
# mysql> GRANT ALL PRIVILEGES ON *.* TO 'rotimi'@'localhost' WITH GRANT OPTION;
# mysql> CREATE USER 'rotimi'@'%' IDENTIFIED BY 'password';
# mysql> GRANT ALL PRIVILEGES ON *.* TO 'rotimi'@'%' WITH GRANT OPTION;
@rotexdegba
rotexdegba / handy-regexes.txt
Last active Dec 31, 2015
Useful regular expressions
View handy-regexes.txt
Remove spaces after last semi colon on each line of a php file.
find:
;[^\S\n]+$
relace with:
;
---------------------------------------------------------------------------------------
Remove spaces on blank lines
find:
^[^\S]+$
replace with:
@rotexdegba
rotexdegba / for-loop-replacement-of-foreach.php
Last active Apr 25, 2016
for loop replacement of foreach
View for-loop-replacement-of-foreach.php
<?php
for (reset($array); list($key, $value) = each($array);) {
echo $key;
echo $value;
echo $array[$key];
}
//You can add more initializations, conditions and incrementation statements
//e.g
You can’t perform that action at this time.