Writing
Notes on building software — the decisions that age well, the ones that don't, and what I learn in between.
-
How to Cast an Array to an Object with PHP
It's short and sweet. $objfromarray = (object)['key'='val']; echo $objfromarray-key;
-
How to Tag and Push a Release with Git
Set the -a and the -m flags like so git tag -a v1.0.0 -m "Note about the release goes here" Then to push the tag to a repository git push origin --tags And that's it! Here are the docs...
-
How to Make an iOS App Icon from a File on the Command Line or Web Browser
If you need to make iOS app icon assets from a photo, take a look at the Make App Icon website and API. They will let you upload an image from a web browser (in exchange for an email) and then...
-
Simple Way to Calculate the Odds of Something Happening
Here is a quick way to express a chance of something happening. This is an example in JavaScript that gives you a 1 in 3 chance of being true. [true, false, false].sort(function(){ return...
-
PHP Headers for Sending CSV File Downloads
If you would like to force a file download prompt to the user, instead of just outputting the text to the browser, use the following headers. <?php $filename = "some-filename";...
-
XPath with HTML in PHP One Liner
Here is a one liner for using XPATH with HTML in PHP $doc = new DOMXPath(@DOMDocument::loadHTML(filegetcontents("https://www.reddit.com/r/PHP/"))); Now you can use XPATH to query the html.....
-
How to Calculate Age with PHP
Calculate age in PHP $yourbirthday = '1982-08-29'; $difference = datediff(datecreate(), datecreate($yourbirthday)); $age = $difference-format('%Y'); // 35 Short and sweet
-
How to Use Named Variables with Postgres and PHP PDO Driver
You can write reusable scripts with Postgres by taking advantage of named variables. A named variable starts with a : in your sql script. Here is an example select :anumber You can then use this...
-
How to Get a Random Item from an Array in PHP
Use this snippet for grabbing a random item from an array in php $fruits = ['apple', 'banana', 'carrot', 'date', 'elderberry']; echo arrayrand(arrayflip($fruits)); // = 'banana' PHP's arrayflip...
-
Connect to Postgres on Heroku using DATABASE_URL Config Var with PHP and PDO
Unfortunately PHP's PDO constructor doesn't take a database connection url (in a format that Heroku makes available as a config var) as an argument. It has its own, rather odd syntax. However, it's...
-
How to Slugify a String in PHP
Here is a snippet for generating a URL friendly slug in PHP function slugify($string){ return strtolower(trim(pregreplace('/[^A-Za-z0-9-]+/', '-', $string), '-')); } And you can use it in your code...
-
How to Generate a UUID in PHP 7
Here is a little snippet for generating a UUID in PHP 7 function uuid(){ $data = randombytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return...
-
Apache Rewrite Rule for Mapping to index.php in Sub Folders
Below is an example URL structure with rewrite rules using an Apache .htaccess file. GET /a/ = a/index.php GET /a/b/ = a/b/index.php GET /a/b/c/ = a/b/index.php This URL mapping will give you...
-
Manual ManyToMany Through with Django's ORM
Here is a code snippet that demonstrates how to set up a ManyToMany through relationship in Django. In Rails, the equivalent would be called a hasmany through association. If you set the through...
-
How to Read Response Body HTML with Javascript Fetch Method with React Native
In React Native you see a lot of tutorials and articles using the fetch method when interacting with JSON APIs. It's great for this and the the pattern looks like this fetch(url).then((resp)={...