# Reverse A Number in PHP with Various Methods

In PHP We don’t have to declare the data type of a variable. So in PHP, it is easier for us to Reverse a number using the strrev() function.

Using this function we can reverse any number with just a single line code. This function strrev(), here you can see the function name str is generally stands for string, we all know that. So while using this function, the function just simply make it reverse.

But In this tutorial, I am going to use a proper algorithm to reverse a number, as strrev() function has several limitations.

At the end of this tutorial, I am going to use strrev() to make you understand what are the limitations of using strrev().

### PHP Program To Reverse A Number

The below code is for reverse number in PHP

```<?php

\$number = 12345;
\$reverse = 0;
while (\$number != 0)
{
\$reverse = \$reverse * 10 + \$number % 10;

\$number = (int)(\$number / 10);
}

echo \$reverse;

?>```

`\$number = (int)(\$number / 10);`

(int) is used to round off the value and it is very essential in order to achieve our goal.

in while loop, I have used \$number variable to reach it to zero so that our loop get stopped when reaching the last digit of our number.

Main Logic:

define a variable for the reversed number and initialize it with zero.

Then Just simply multiply the variable by 10 and add the remainder which we got after dividing the number with 10.

PHP Program To Find The Largest Number of Given Values

Check if a string contains a particular word using PHP

### How to reverse number in PHP using the library function

```<?php

\$number = 12345;

echo strrev(\$number);

?>```

So, you have found that strrev() function is very easy and fast way to reverse a number in PHP.

Even you can reverse a string too using the same function. But It does not change anything to the original value. This function just returns the reversed string or number that’s all.

How to Count The Sub String from a .txt file using PHP