Monday, June 3, 2013

Reverse number

Question: We have to ask user to enter number he/she wants, and number should be generated reverse accordingly.
(if user enter number 12345 then reverse should be generated i.e.- 54321)

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Reverse number</title>
    </head>

    <body>
        <h1><center>Reverse number</center></h1>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            Enter number more than one digit:<input type="text" name="rev" />
            <input type="submit" name="submit" value="OK" />
        </form>
        <?php
            if (isset($_POST['submit'])) {
                $rev = $_POST['rev'];
                while ($rev >= 1) {
                    $r = $rev % 10;

                    $rev = $rev / 10;

                    echo $r;
                }
            }
        ?>
    </body>
</html>

No comments:

Post a Comment