How to Reverse a Number in PHP

How to reverse a number using PHP code. This question is asked by one of our blog follower on webrewrite facebook page.  In this tutorial, We’ll learn how to reverse a number by creating our own custom method.

There are multiple approaches you can use to solve this problem. PHP doesn’t provide any inbuilt function to reverse a number directly.

How to sort a string in PHP

Check whether a number is prime or not

How to Reverse a Number in PHP

METHOD 1– Reverse a number without using any inbuilt functions.

Let’s create a simple function which takes a number as an input and return the reverse of a number.


Subscribe Our Tutorials

Get Latest Updates on Facebook

PHP video tutorials

METHOD 2 – Reverse number using inbuilt functions.

In this approach, we use little trick as PHP doesn’t provide any inbuilt function for reversing number directly.

a) Using str_split() and array_reverse()


Top five most used PHP functions for beginners

b) Using strrev() method.


I demonstrated three approaches to reverse a number in PHP. If you know some other method then please let us know through your comments.


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.