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:
- 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.
- $rem is then added to the variable $rev after multiplying it by 10.
- Multiplication by 10 adds a new place in the $rev contains. $rev contain like this 0 * 10 + 3 = 3.
- $num is then divided by 10 so that now it only contains first two digits: 12.
- After second iteration, $rem equals 2, $rev equals 3 * 10 + 2 = 32 and $num = 1.
- After third iteration, $rem equals 2, $rev equals 32 * 10 + 1 = 321 and $num = 0.
- Now, $num will be existed outside the while loop and $rev contains 321.
Now , look at this coding
Input : 12345
Thanks
PHP Fundamental Tutorials with Basic Demo by Chentan in 2020 – Part-1
PHP Fundamental Tutorials with Basic Demo by Chentan in 2020 – Part-2
Latest posts by Usha Kiran (see all)
- How to Login with Token in Laravel PHP Framework? - October 30, 2021
- How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-4) - October 29, 2021
- How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-2 - October 29, 2021