How to print the number in reverse order in PHP embedded in HTML? -4

How to print the number in reverse order in PHP?

In this program, you’ll learn to reverse a number using a while loop.

Logic:

  1. First of all, the remainder of $num divided by 10 is stored in the variable $rem. Now, $rem contains the last digit of $num, i.e. 3.
  2. $rem is then added to the variable $rev after multiplying it by 10.
  3. Multiplication by 10 adds a new place in the $rev contains. $rev contain like this 0 * 10 + 3 = 3.
  4. $num is then divided by 10 so that now it only contains first two digits: 12.
  5. After second iteration, $rem equals 2, $rev equals 3 * 10 + 2 = 32 and $num = 1.
  6. After third iteration, $rem equals 2, $rev equals 32 * 10 + 1 = 321 and $num = 0.
  7. Now, $num will be existed outside the while loop and $rev contains 321.

Now , look at this coding

Input : 12345

Output :54321

Thanks