Code Snippets

Author: chris

PCRE Regex in PHP

Use preg_match with named groups to parse structured text.

Author: chris

Hash Passwords with bcrypt

Use password_hash and password_verify — never store plaintext.

Author: chris

JSON Encode and Decode in PHP

Round-trip JSON with JSON_THROW_ON_ERROR for safe decoding.

Author: chris

cURL GET Request in PHP

Issue a GET request with libcurl wrappers and capture the body.

Author: chris

Write to a File in PHP

Use file_put_contents with LOCK_EX for an atomic write.

Author: chris

Read a File Line by Line in PHP

Use SplFileObject to iterate a file lazily without loading it all into RAM.

Author: chris

Wrap and Unwrap Errors in Go

Wrap errors with fmt.Errorf %w and inspect with errors.Is / As.

Author: chris

Connect to PostgreSQL in Go

Open a database/sql handle to Postgres and query rows.

Author: chris

Minimal HTTP Server in Go

Spin up a net/http server with a JSON-returning handler.

Author: chris

Parse Flags with the flag Package

Parse typed CLI flags using the stdlib flag package.

Author: chris