Home >Backend Development >PHP Tutorial >Quickly reverse a positive integer and output it through a PHP program

Quickly reverse a positive integer and output it through a PHP program

藏色散人
藏色散人Original
2021-08-10 10:22:156000browse

Give you a number 1234567, can you write a program to output 7654321? This is the so-called inverted output or reverse sequence output. I wonder if any of you, as programmers, have any ideas for implementation?

First of all, I will give you two ideas:

1. The recursive function passes in a positive integer, first outputs the last number, then divides it by 10 and uses the recursive function to continue iterating. The recursion is terminated when the positive integer is 0.

2. Output an integer in reverse order, divide it by 10 until it is 0, and output the remainder of 10. The final result is the reverse order of this integer.

You can practice locally first, and then compare the methods I gave.

The following is the method I gave:

The PHP code is as follows:

<?php

function reverse_integer($n)
{
    $reverse = 0;
    while ($n > 0)
    {
        $reverse = $reverse * 10;
        $reverse = $reverse + $n % 10;
        $n = (int)($n/10);
    }
    return $reverse;
}
print_r(reverse_integer(1234)."<br>");
print_r(reverse_integer(23));

Output results:

4321
32

Introduction to relevant knowledge points:

Loop statement while: As long as the specified condition is true, the loop code block;

x % y: modulo (remainder of division), the remainder of x divided by y;

x / y: division, quotient of x and y;

x * y: multiplication, product of x and y;

print_r() function: used to print variables for easier understanding display in the form.

(int) Usage example:

$a=0.99; 
$b=(int)$a; //$b=0; 
$a=1.99; 
$b=(int)$a; //$b=1;

Attached is an introduction to int:

int is a set ℤ = A number among {..., -2, -1, 0, 1, 2, ...}.

Integer value int can be represented in decimal, hexadecimal, octal or binary, and can be preceded by an optional symbol (- or ). You can use the negative operator to represent a negative int.

To use octal expression, 0 (zero) must be added before the number. To use hexadecimal representation, the number must be preceded by 0x. To use binary representation, the number must be preceded by 0b.

Starting from PHP 7.4.0, integer values ​​may contain underscores (_). For a better reading experience, these underscores will be filtered out by PHP when displayed.

Finally, I would like to recommend to you the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Quickly reverse a positive integer and output it through a PHP program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn