Tag: PHP
-
Basic HTTP Authentication with PHP
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…
-
Life of Pi – Working with Pi in PHP
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…
-
How to replace a string with a string in PHP
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…
-
How to round numbers with PHP
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…
-
Converting decimal to other number systems with PHP
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…
-
To infinity and beyond! Working with infinity in PHP
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…
-
Finding the smallest or largest value with PHP
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…
-
Find the largest value in an array with PHP
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…
-
Find the lowest value in an array with PHP
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…
-
How to convert an array to a string with PHP
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…