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.

One Response to How to Reverse a Number in PHP

  1. Arthur Kushmantsev says:

    What – why are U doing all this?
    Why not using arithmetic approach like this:

    function reverseNum(int $n, int $i = 0)
    return ($n === 0) ? $i : reverseNum($n/10, $i*10 + $n%10);
    Even if U want to use string, then why are U using so many heap with strrev() function, why not just return result with cast?

    function reverseNum($num)
    return (int)strrev($num);

Leave a Reply

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