Skip to content

Instantly share code, notes, and snippets.

@lcherone
lcherone / guidv4.php
Last active December 7, 2017 19:30
guidv4 with random_bytes()
<?php
function guidv4()
{
if (function_exists('random_bytes') === true) {
$bytes = random_bytes(16);
} elseif (function_exists('openssl_random_pseudo_bytes') === true) {
$bytes = openssl_random_pseudo_bytes(16);
} elseif (function_exists('mcrypt_create_iv') === true) {
$bytes = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
} elseif (function_exists('com_create_guid') === true) {
@lcherone
lcherone / bug.php
Last active December 13, 2017 11:31
Addition precedence post increment bug.
<?php
$a = 0; echo $a+$a+$a++; //0
$a = 1; echo $a+$a+$a++; //3
$a = 2; echo $a+$a+$a++; //6
$a = 3; echo $a+$a+$a++; //9
echo PHP_EOL;
$a = 0; echo $a+$a++; //1 <-- should be 0
$a = 1; echo $a+$a++; //3 <-- should be 2
@lcherone
lcherone / hash.php
Created December 14, 2017 19:44
Basic encode an int into a short hash.
<?php
/**
* Encode an int into a short hash.
*
* @param int $id
* @param string|int $seed - Lock alphabet shuffle, or leave blank for random
* @return string
*/
function id_encode(int $id, $seed = null) {
$alphabet = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
@lcherone
lcherone / recursive_menu_function.md
Created December 24, 2017 05:20
nested recursive menu function
@lcherone
lcherone / c9php7.sh
Created December 24, 2017 08:09
Cloud9 upgrade PHP 5 to 7
#!/bin/bash
# add ppa and update
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
# install deps
sudo apt-get install -y php7.0-{dev,curl,gd,intl,mcrypt,json,mysql,opcache,bcmath,mbstring,soap,xml}
sudo apt-get install -y libapache2-mod-php7.0
@lcherone
lcherone / adminer.md
Last active January 3, 2018 23:14
Adminer download

downloads and names the file adminer.php

wget http://www.adminer.org/latest.php -O adminer.php

downloads and names the file adminer-4.2.1.php (E.G with its version number)

wget -N --content-disposition http://www.adminer.org/latest.php

You can achieve this by using the [modulus operator][1].

<?php
$result = [
    ['id' => 1, 'product_name' => 'a'],  
    ['id' => 2, 'product_name' => 'b'],  
    ['id' => 3, 'product_name' => 'c'],  
    ['id' => 4, 'product_name' => 'd'],  
    ['id' => 5, 'product_name' => 'e'],  

['id' => 6, 'product_name' => 'f'],

@lcherone
lcherone / 33.php
Created January 18, 2018 17:23
Tiny procedural MVC example, with template and nested views.
<?php
// passed from rewrite
$_GET['controller'] = ''; // index page
// controllers map
$controllers = [
'' => 'index.php',
'foobar' => 'foobar.php'
];
{
"current": [
{
"version": "Ubuntu 17.10",
"codename": "Artful Aardvark",
"release_date": "October 19, 2017",
"end_of_life": "July 2018",
"links": [
"https://wiki.ubuntu.com/ArtfulAardvark",
"https://wiki.ubuntu.com/ArtfulAardvark/ReleaseNotes",
@lcherone
lcherone / write_ini_file.md
Created January 24, 2018 23:31
PHP write_ini_file implementation

Below is an implementation of write_ini_file() which PHP is currently lacking, it will create an almost identical (except comments) of the input:

  • Supports cross platform (PHP_EOL) new lines added between sections.
  • Handles both index and key value arrays.
  • Handles CONSTANT style values.
  • And file locking to stay consistent.

Source

php