How to Reverse a Number in PHP

How to reverse a number in PHP. In this tutorial, you are going to learn how to write a code to reverse a number in php without using function (in-built functions).

There are multiple approaches you can use to solve this problem. PHP doesn’t provide any in-built 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 Function

In this example, we write a PHP code to reverse a number without using function. We write a logic which take number as an argument and reverse that number.

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

 

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 – PHP program to Reverse a number using in-built functions.

In this approach, we use a little trick to reverse a number. As PHP doesn’t provide any inbuilt function for reversing a 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.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.