Get all defined variables

I’ve never actually used this function, but could definitely see using it to help profile a system and/or to help identify defined but unused variables. To obtain an array of all of the defined variables you would: $defined = get_defined_vars(); The function returns a multi-dimensional array of all of the defined variables. This is not limited to used-defined variables, so you will end up...

Read Full Article →

Working with JSON

JSON is one of my favorite human readable formats. It’s widely used and has great support in PHP as well as other languages. PHP allows you to easily convert variables into JSON and JSON into objects or arrays. First, let’s take a look at how we can convert an array into JSON: $array = ['var' => 'val', 3 => 14, [1,2,3]]; $json = json_encode($array);...

Read Full Article →

Truncate string with ellipses

We previously discussed how to calculate the length of a string in PHP and I made mention that using that function is how you would go about truncating a string if it’s over a specific length. I also said that down the road I would discuss the topic at hand, truncating a string with ellipses. Here is a pretty simple function to do just...

Read Full Article →

Get date for Easter in PHP

All the good things about Easter aside, it’s one of those holidays that’s a pain to get the date for. This is because the date of Easter is based on the lunar cycle and Spring Equinox (March 21st). The date of Easter on a given year could fall between March 22nd and April 25th. Fortunately PHP has us covered with a pair of functions,...

Read Full Article →

Serializing and unserializing variables

Serialization in PHP is the act of converting a variable into a storable value. When I say storable, I mean being able to store the result in say, a database or a flat file. This is commonly applied to arrays and objects as they are not simply text strings. Under the hood, PHP uses serialize() for session data as it is just an array....

Read Full Article →