Useful PHP Snippets

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.

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.

$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():

// 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:

// 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.

// 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.

// 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.

Content Freeze

As part of the ongoing upgrades, SR Info will be migrating to a new server. Please refrain from any of the following:

  • Directory Profile Updates ( Bio edits and Picture uploads)
  • SR Info Site Updates (If you manage an SR Info site)
  • HB2504 File Uploads (Syllabi and CVs)

The Content Freeze will remain in place until Monday, March 2nd, at that time you will be able to proceed with any of the above.

If you encounter any issues after Monday, please contact webtech@sulross.edu and we will engage accordingly.

Thank you,
-Web Services