Page 2 of PHP Articles
Converting a string to a timestamp is one of my favorite things about PHP. In fact when trying to decide which language to build [HolidayAPI][holidayapi] in I couldn’t find an implementation of PHP’s strtotime() that rivaled the original. The strtotime() function takes a textual string as input and then spits
[…]
PHP arrays are absolutely fantastic. They hold stuff, doesn’t really matter what. The interfacing is pretty consistent and there’s a boat load of built-in array functions. What PHP lacks is the ability to set the array pointer to an arbitrary key or value. There are ways to move forward and
[…]
Last week we talked about setting up a local development server with PHP’s built-in web server and I mentioned that we’d delve into page routing. Routing refers to taking the URI that a person was requested, let’s say /about and routing that to the appropriate code. Sure, you could just
[…]
I recently updated [HolidayAPI][holidayapi] to no longer use my PHP framework because I wanted the system to be easier for new developers to get up and running. Instead of including configuration files for Apache or nginx, I decided that I should just use the web server that’s baked right into
[…]
Variable variables are one of my favorite things about PHP. PHP allows you to use a variable to reference another variable. This comes in exceptionally handy when you need to create variable names dynamically: $variable = 'This is my variable'; $var = 'variable'; echo $$var; Let’s say you have you
[…]
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
[…]
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
[…]
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,
[…]
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
[…]
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
[…]
Calculating the length of a string is a useful feature of any language, in fact it’s one of the few pieces of functionality that has a consistently simple syntax across modern languages. You can calculate the length of a string as part of some server side user input validation or
[…]
The hostname of your server or local system is an easy way to determine which environment you are working with, either local or production. Prior to PHP 5.3 you would need to utilize the php_uname() function and with 5.3+ there is a built-in function for getting the hostname: pre-5.3 $hostname
[…]
You may already be familiar with file_get_contents() for reading the contents of a local file but did you know that you can use it to read the contents of a remote file or site? You can then use file_put_contents() to write the file’s contents back to disk: file_put_contents('./some/local/file', file_get_contents('http://phpave.com')); Both
[…]
Basic HTTP Authentication is easily accomplished at the web server level (by way of .htaccess with Apache or inside your nginx configuration file) but did you know you could pull it off inside of a PHP script? You bet your butt you can! First, you will need to interrogate the
[…]
The irrational mathematical constant of Pi can be obtained a few different ways in PHP. There is a function to give you the value as well as a bunch of constants that represent Pi as well as fractions of Pi and it’s square root. Heck, you could even do it
[…]
String replacements in PHP are very simple and as per usual, can be done in a variety of different ways. First let’s take a look at the most simple form of a string replacement: str_replace('blue', 'red', 'My favorite color is blue'); The first argument is the string you want to
[…]
Rounding numbers can be done a number of ways. You can round a float up or down to the nearest integer or you could round to a specific precision. Even then you can choose whether or not to round halves up or down and even round towards even and odd
[…]
It felt a bit like cheating to split this post up so I figured I would just cover all of the decimal to… functions in one post. Decimal, also known as base ten, is the numerical base we are most accustomed to. From time to time we need to convert
[…]
We can file this one under the “PHP functions I never knew existed and probably will never use” category. Seems that as of PHP 4.2.0 and beyond there was the inclusion of an is_finite() and an is_infinite() function. The names are pretty self explanatory and I’m sure there are some
[…]
Figured I would keep up the trend of talking about finding the smallest and largest values in arrays by talking about finding the smallest or largest value in a set of values. Incidentally, doing so will utilize the same min() and max() functions we used previously. Not one of my
[…]
Last week we discussed how to find the smallest value in an array so it seemed fitting to discuss how to find the largest value in an array. As we previously discussed back in the day you would have to loop through an array and compare values until you found
[…]
Finding the lowest value of an array is a pretty common task in PHP, especially when you are dealing with things like scores or money. In the old days before PHP 4, finding the lowest value of an array meant looping through the array and comparing values until you had
[…]
Converting an array to a string is a pretty frequent task when programming. Whether you want to join some sentences into a paragraph or just smash the values into some sort of hash, you can do so very simply with PHP’s implode function. Implode takes 1 to 2 arguments. In
[…]
The holidays are upon us so it’s time for my obligatory holidays themed post! Keep in mind that the title may say Christmas but you could apply this same logic to any holiday or date of your choosing. $christmas = date('Y-12-25'); $today = date('Y-m-d'); echo (strtotime($christmas) - strtotime($today)) / 86400;
[…]
Reversing a string in most languages is a pretty trivial task. In PHP we have a dedicated strrev function to do so: $string = 'This is my awesome string!'; $reverse = strrev($string); But what if this function didn’t exist? How would you go about reversing the string? For me, I’d
[…]