PHP & Frameworks

How to reverse number in PHP without using function?

To reverse a number in PHP without the use of integrated function. First we want to claim a variable to save the reversed number, and first of all assign 0 value to it. Now, extract the final digit from the unique number, and multiply the opposite number with 10, then add the extracted digit to reversed number, and divide the unique number by 10.

A number can be written in reverse order.

For example

12345 = 54321

/* PHP program to reverse a number without using function */ 
$num = 12345;
$revnum = 0;
while ($num != 0)
{
 $a = $num % 10;
        $revnum = ($revnum * 10) + $a
 //below cast is essential to round remainder towards zero
 $num = (int)($num / 10); 
} 
echo "Reverse number: $revnum\n";

In the above example we reverse the number without using PHP function. This is very important technical php interview questions you should know this trick to reverse the number.

Now we look using PHP function reverse the number

$num = 1234567;
$rev =  strrev($num);

echo "original number is =  <b> $num </b> <br/>";
echo "reverse number is =  <b> $rev </b> <br/>";

Related Articles

Password Strength Checker Using JavaScript

How To Find Reverse A Number In Python

How To Efficiently Remove Items From An Array In JavaScript

The Power Of Free Keyword Research Tools

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Laravel vs Node Js: Which One Is Good For What?

Introduction In the world of web development, selecting the correct framework is critical. Laravel and…

3 months ago

Docker Cheatsheet: Essential Commands and Explanations

Introduction By enabling containerization, Docker has transformed the way software is built, deployed, and managed.…

8 months ago

Difference between Memcached and REDIS – Secrets of Caching

Introduction Speed and efficiency are critical in the ever-changing world of web development. Effective caching…

8 months ago

How to Revert a Git Commit: A Simple Example

Introduction Git, a popular version control system in software development, enables developers to track changes,…

8 months ago

What is Git Stash and Why Do You Need It

Introduction Are you tired of grappling with tangled changes and unfinished work while using Git?…

8 months ago

Difference between git stash pop and git stash apply

Introduction Version control is essential in software development because it ensures that changes to a…

8 months ago