Functions Archives

Formatting numbers with sprintf

In the game Keno you might have noticed that games begin at 001 and iterate to 999. Let’s look at some code to cycle properly and stay formatted in three digits using sprintf().

$game_number = 1;
echo ‘Game #’ . sprintf(“%03d”,$game_number); // Game #00x
 

Now let’s add some style magic to give the output a blackground with […]

How to use MySQL REPLACE function

Here’s a MySQL query fuction that’s easy to forget about: REPLACE. Let’s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here’s the syntax to update:

UPDATE tdurl_1 SET URL = REPLACE(URL, ‘http://www.tdurl.com/’,’http://tdurl.com/’);

Debugging numerical sorting with strings

Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers.
In the meantime a good question came in on the sorting post back in October from Beregszászi Mihály, who wrote:

I have got a problem:
rsort($numbers);
Print:
9
8
7
70
6
69
68
67
This is my problem! My […]

How to pluralize using PHP

This is one of those technical nitpick things. Check out the screenshot below of the new Yahoo Answers and see if you can spot the error.

The title of this thread kind of gives it away. Note the “1 Answers” should actually read “1 answer” and then when there ar 2 or more it should […]

Converting domains to cell phone numbers

Awhile back I created a basic webmaster utility script called Easy Cell Code Generator which helped find easy to type cell phone numbers, but what if you had a domain and wanted to convert it out the other way? I decided to whip up some quick code to do just that:
Short domain:
mughd.com
Typed into cell […]

pathinfo

Let’s say you have a long URL or file and want to access different parts. PHP has a built-in function called pathinfo() that may be of assistance. Let’s look at a couple examples.
File path
$file_path = ‘/usr/web/tdavid/php-scripts.com/main/index.html’;
URL
$url_path = ‘http://www.php-scripts.com/main/index.html’;
Now let’s dump this into a script and see the output:

<?php
//File path
$file_path = pathinfo(‘/usr/web/tdavid/php-scripts.com/main/index.html’);
//URL
$url_path = pathinfo(‘http://www.php-scripts.com/main/index.html’);
print ‘dirname: ‘ […]

ASCII character codes

If you need to print out special characters in PHP you will probably want to get familiar with the built-in chr() function. Here’s some code that will demonstrate the various ASCII character codes available and how to output them to the screen:

<?php
print ‘Copyright (169): ‘ . chr(169);
print ‘<br />Registered Trademark (174): ‘ . chr(174);
print ‘<br […]

Using file_get_contents before v4.3.0

One of the dangers of using newer PHP functions is that sometimes you’ll end up with people who are stuck on a shared host using an older version of PHP. In this case it’s helpful to have some backwards compatible functions. The category backwards compatability will deal with these situations.
file_get_contents has only been available in […]

Is number odd or even?

Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn’t even necessary and could be expressed as a one-liner like this:

echo($number & 1); // $number = any integer, 0 = even, 1 = odd
 

Here’s the code to check […]

Brief explanation of how to use arguments and globals inside functions

The concept of globalization, functions and arguments can be confusing to those who are new to programming. It’s not too complicated if you look at a very basic example. The following function isn’t very practical, but it works.

<?php
function addThis($a,$b) {
return $a + $b;
}
$sum_of_these = addThis(8,3); // assigns the value of 11 to the variable $sum_of_these
?>

In […]