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

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

3 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

4 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

4 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

4 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

6 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

8 months ago