Sometimes little bits of PHP can make your life easier. The following are a few helpful little bits of code you can use in your webpages.
Add an ordinal to the end of a number
I’ve seen several of these and some are more elegant than others. This isn’t one of the most elegant in my opinion, though it is a function.
1 2 3 4 5 6 7 8 9 10 |
function ordinal($cdnl){ $test_c = abs($cdnl) % 10; $ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th' : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) > ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $cdnl.$ext; } for($i=1;$i<100;$i++){ echo ordinal($i).'<br>'; } |
Here’s one that I think is better. Note that it doesn’t have brackets in statements.
1 2 3 4 5 |
$ends = array('th','st','nd','rd','th','th','th','th','th','th'); if (($number %100) >= 11 ($number%100) = 13) $abbreviation = $number. 'th'; else $abbreviation = $number. $ends[$number % 10]; |
Serialization
Sometimes you need to convert your arrays or objects into formatted strings so that you can do other things with the data. This comes from the article 9 Useful PHP Functions and Features You Need to Know.
There are two popular methods of serializing variables. Here is an example that uses the serialize() and unserialize():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// a complex array $myvar = array( 'hello', 42, array(1,'two'), 'apple' ); // convert to a string $string = serialize($myvar); echo $string; /* prints a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";} */ // you can reproduce the original variable $newvar = unserialize($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) |
This was the native PHP serialization method. However, since JSON has become so popular in recent years, they decided to add support for it in PHP 5.2. Now you can use the json_encode() and json_decode() functions as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// a complex array $myvar = array( 'hello', 42, array(1,'two'), 'apple' ); // convert to a string $string = json_encode($myvar); echo $string; /* prints ["hello",42,[1,"two"],"apple"] */ // you can reproduce the original variable $newvar = json_decode($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */ |
It is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.
Generating Unique ID’s
Often there are situations where you need to generate a unique string. People frequently use the md5() function for this, even though it’s not exactly meant for this purpose. This comes from the article 9 Useful PHP Functions and Features You Need to Know.
1 2 |
// generate unique string echo md5(time() . mt_rand(1,1000000)); |
There is a PHP function named uniqid() that is meant to be used for this.
1 2 3 4 5 6 7 8 9 10 11 |
// generate unique string echo uniqid(); /* prints 4bd67c947233e */ // generate another unique string echo uniqid(); /* prints 4bd67c9472340 */ |
Even though the strings are unique, they seem similar for the first several characters. This is because the generated string is related to the server time. This has a nice benefit, as every new generated id comes later in alphabetical order, so they can be sorted.
To reduce the chances of getting a duplicate, you can pass a prefix, or the second parameter to increase entropy.