How to Reverse a Number in PHP

How to reverse a number in PHP. In this tutorial,  I am going to explain how to write a program to reverse any number in PHP. Also, we reverse a number in PHP without using any in-built function. 

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

Reverse a Number in PHP without using any in-built Function

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

Subscribe Our Tutorials

Get Latest Updates on Facebook

 

Reverse a Number in PHP

Reverse a Number in PHP

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

 

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.

Conclusion

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.