Skip to content

Instantly share code, notes, and snippets.

View alash3al's full-sized avatar

Mohammed Al Ashaal alash3al

View GitHub Profile
@alash3al
alash3al / parse_str.js
Created February 20, 2015 21:22
Parse a string and split it into key value pairs, the php parse_str()
/**
* Parses the string into variables
*
* @param mixed $string
* @param mixed $equal
* @param mixed $separator
*
* @author Mohammed Al Ashaal <is.gd/alash3al>
* @version 1.0.0
*
@alash3al
alash3al / smtp.php
Last active July 27, 2016 05:33
PHP mail() alternative, smtp()
<?php
/**
* Send a mail using smtp server
*
* @param array $options [from, to, server, tls, subject, message .. etc]
* @param array $headers for custome headers as key value pairs [key is case insensetive]
*
* @author Mohammed Al Ashaal <fb.com/alash3al, is.gd/alash3al, github.com/alash3al>
* @copyright (c) 2015, Mohammed Al Ashaal

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

@alash3al
alash3al / milliseconds.php
Created August 20, 2015 19:30
milliseconds - return current Unix timestamp with milliseconds for php just like javascript's Date.now()
<?php
/*
* Return current Unix timestamp with milliseconds
*
* @author Mohammed Al Ashaal <github/alash3al, fb/alash3al, tw/m7medalash3al>
* @return float "due to INT limitaions"
*/
public function milliseconds()
{
list($a, $b) = explode(' ', microtime());
@alash3al
alash3al / inspectUserLocation.php
Last active September 14, 2015 13:16
Extract the "most valid" ip-address from the client-request, then uses a `GEO-IP` service to fetch its information .
<?php
/*
* Extract the "most valid" ip-address from the client-request
* then uses a `GEO-IP` service to fetch its information .
*
* To extract the "most valid" ip-address, it loops over all http-headers
* from the client-request, then gets the ip that matches this pattern
* "^([0-9]+){3}\.([0-9]+){3}\.([0-9]+){3}\.([0-9]+){3}$"
* this is done becaouse each proxy server "may" send a http-header
* i.e: "HTTP_X_FORWARDED_FOR", "HTTP_VIA" ... etc .
@alash3al
alash3al / downloadCurrent.js
Last active November 2, 2015 18:00
just run it in the console, and the browser will show you the download dialog
// copy & paste in the browser console "F12" and press ENTER
// then the browser will show you the download dialog
// this script makes use of HTML5 'download' attribute
// by Mohammed Al Ashaal <www.alash3al.xyz>
(function(){
dl = document.createElement("a")
dl.href = window.location
dl.download = ""
dl.click()
})()
@alash3al
alash3al / file-downloader-sample.go
Last active January 4, 2016 10:56
just for fun ;), i'm "www.alash3al.xyz"
package main
import(
"fmt"
"net/http"
"os"
"sync"
"io"
)
@alash3al
alash3al / simple-space-ship.html
Created November 25, 2015 11:43
just move+falling-balls animations
<!-- www.alash3al.xyz -->
<style>
* {
margin: 0;
padding: 0;
}
body {
background: #000;
overflow: hidden;
}
@alash3al
alash3al / gist:d1513cdc6d6d6f690295
Created December 31, 2015 04:08 — forked from orangexception/gist:1301150
Minify JavaScript Regular Expression

Notice

Do not run this against minified JavaScript packages. The minified packages often rely on line returns to execute correctly.

This regular expression was designed to minify simple JavaScript.

Regular Expression

(?s)[\t\r\n]|[ ][ ]+|/\*.*?\*/*

I put spaces in square brackets as a reminder that they exist. Spaces can be important. For example, $( "#foo #bar" ) should not become $("#foo#bar"). However, we do want to remove spaces if they are used for indentation.

@alash3al
alash3al / timeDiff.js
Created December 31, 2015 22:07
get the difference between two unix dates in the style of "x [second|minute|hour|month|year]s ago"
// timeDiff - get the difference between two "unix" dates
// version: 1.0.0
// author: Mohammed Al Ashaal
function timeDiff(t_new, t_old) {
t = {
second: 1,
minute: 1 * 60,
hour: 1 * 60 * 60,
day: 1 * 60 * 60 * 24,
week: 1 * 60 * 60 * 24 * 7,