How to Reverse a Number in PHP

View: 303    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Php&mySql   Fields: Other

0 point/1 review File has been tested

How to reverse a number using PHP code. This question is asked by one of our blog follower onwebrewrite facebook page. In this tutorial, We’ll learn how to reverse a number by creating our own custom method.

Introduction

How to reverse a number using PHP code. This question is asked by one of our blog follower onwebrewrite facebook page.  In this tutorial, We’ll learn how to reverse a number by creating our own custom method.

There are multiple approaches you can use to solve this problem. PHP doesn’t provide any inbuilt function to reverse a number directly.

How to Reverse a Number in PHP

METHOD 1– Reverse a number without using any inbuilt functions.

Let’s create a simple function which takes a number as an input and return the reverse of a number.

 /* Return reverse of a number.
    @param int number
  */
 
 function reverse_number($number){
 
    $sum = 0;
 
    while( floor($number) ) {
 
        $newnum = $number % 10;
        $sum = $sum * 10 + $newnum;
  
        $number = $number/10;
    }
 
   return $sum;
}
 
echo reverse_number(5674);
 
/* Output - 4765 */

METHOD 2 – Reverse number using inbuilt functions.

In this approach, we use little trick as PHP doesn’t provide any inbuilt function for reversing number directly.

a) Using str_split() and array_reverse()

/* Return reverse of a number.
    @param int number
  */
 
 function reverse_number($number){
    
    /* Convert them into an array. */
 
    $arr = str_split($number);
 
    /* Reverse the array. */
 
    $rev_arr = array_reverse($arr);
 
    /* Implode them. */
    $rev = implode("",$rev_arr);
 
   return $rev;
}
 
echo reverse_number(5674);

b) Using strrev() method.

function reverse_number($number){
    
   /* Typecast the number into string. */
 
    $snum = (string) $number;
 
    /* Reverse the string. */
 
    $revstr = strrev($snum);
 
    /* Typecast string into int. */
 
    $reverse = (int) $revstr;
 
     return $reverse;
}
 
 echo reverse_number(5674);

Conclusion

I demonstrated three approaches to reverse a number in PHP. If you know some other method then please let us know through your comments.

How to Reverse a Number in PHP

How to Reverse a Number in PHP Posted on 24-09-2016  How to reverse a number using PHP code. This question is asked by one of our blog follower onwebrewrite facebook page. In this tutorial, We’ll learn how to reverse a number by creating our own custom method. 5/10 303

Comment:

To comment you must be logged in members.

Files with category

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 4071    Download: 0   Comment: 0   Author: none  

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    Category: Php&mySql
    Fields: Other

    1.6666666666667/3 review
    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    View: 395    Download: 0   Comment: 0   Author: none  

    How to MySqli Tutorial PHP MySqli Extension

    Category: Php&mySql
    Fields: Other

    0/0 review
    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    View: 377    Download: 0   Comment: 0   Author: none  

    Make Laravel Artisan Commands

    Category: Php&mySql
    Fields: Other

    0/0 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

  • Check if a Number is a Power of 2

    View: 343    Download: 0   Comment: 0   Author: none  

    Check if a Number is a Power of 2

    Category: Php&mySql
    Fields: Other

    1.5/3 review
    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    View: 403    Download: 0   Comment: 0   Author: none  

    Concatenate columns in MySql

    Category: Php&mySql
    Fields: Other

    0/2 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow

  • How to Query NULL Value in MySql

    View: 336    Download: 0   Comment: 0   Author: none  

    How to Query NULL Value in MySql

    Category: Php&mySql
    Fields: Other

    5/1 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • How to Abstract Class in PHP

    View: 374    Download: 0   Comment: 0   Author: none  

    How to Abstract Class in PHP

    Category: Php&mySql
    Fields: Other

    0/0 review
    What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

  • Use Enums in Rails for Mapped Values

    View: 337    Download: 0   Comment: 0   Author: none  

    Use Enums in Rails for Mapped Values

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a...

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd