Google+ PHP TUTORIALS ONLINE HELP FOR MEDICAL AND PROGRAMMING: Reverse String, without strrev()

Monday 24 March 2014

Reverse String, without strrev()

Reverse String, without strrev()

In this example we have discus how to print  Reverse String, without strrev() function .Fist off all we Assign the given string to the variable and Calculate the length of the string. We use Iterate the string using for loop with appropriate looping and count Concatenate the string inside the for loop. you can see below example.

Example

   <html>
   <head>
   <title>Reverse</title>
   </head>
    <body>
    <?php
            $string = 'Hello php';
                 $length = strlen($string);
           for ($i=($length-1) ; $i >= 0 ; $i--) {
       echo $string[$i];
       }
      ?>
</body>
</html>

Output:  php olleH

PHP strrev() Function

We can also print without loop

< html>
<html>
<body>

<?php
     echo strrev("Hello World!");
     ?>

</body>
</html>

How to reverse a number in php without built(strrev()) in functions

In this example First off all we get the given number and extract the final digit. Initially the reverse of the number will be zero, on each iteration multiply the reverse with 10 and add the modulus result.you can see below example.

Example

 <html>
 <head>
 <title>reverse number</title>
 </head>
 <body>
      <?php
               $number = 1234; $x =0; $y =0;
                  $originalnum = $number;
                  while($number > 1) {
            $x = $number %10;
              $y = ($y * 10)+ $x;
             $number = $number /10;
           }
          Echo "Original Number: ". $originalnum;
          Echo " Reverse : ". $y;
          ?>
</body>
</html>

Output: Original Number: 1234 Reverse : 4321 

No comments:

Post a Comment