Back to Top

How to Get English Ordinal Number Suffix in PHP

PHP Get English Ordinal Number Suffix

A post after very long time. Actually busy with work, here I am going to share one quick post about how to get English Ordinal Number Suffix in PHP.

While working, I have written one function to implement English ordinal numbers (1st, 2nd, 3rd) suffixes using PHP.so today I am going to explain this function to all my readers.

function getEnglishOrdinalSuffix($n) 
{ 
	if (!in_array(($n % 100),array(11,12,13)))
	{
		switch ($n % 10)
		{
        // Only Handle 1st, 2nd, 3rd from Here
        case 1:  return $n .'st';
        case 2:  return $n .'nd';
        case 3:  return $n .'rd';
      }
	}
	return $num.'th';
}

$list = ''; 
for ($n = 1; $n < 150; $n++) { 
	$list .= "$n: " . getEnglishOrdinalSuffix($n) . "\n"; 
}
print "$list";

Well, I wanted to display numbers as follows

1 as 1st, 2 as 2nd and so on to 150 as 150th.

So I have written above code which uses a switch to test the numbers and add the suffix.This article may be obsolete, but still is useful for reference and You can use this script as a quick solution

Hope this post will helpful for you, waiting for your responses.Thanks for reading and feel free to share your thoughts! Don't Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Function handling Functions in PHP

Posted on 7 years ago

Bhumi

Create Custom Page Tabs for Facebook Page

Posted on 7 years ago

Bhumi

Learn Responsive Web Design Online

Posted on 6 years ago

Bhumi

How do I add items in Top navigation bar?

Posted on 5 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi