Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php Write a program to reverse an array or string

<?php
// Iterative PHP program
// to reverse an array
 
/* Function to reverse
$arr from start to end*/
function rvereseArray(&$arr, $start,
                       $end)
{
    while ($start < $end)
    {
        $temp = $arr[$start];
        $arr[$start] = $arr[$end];
        $arr[$end] = $temp;
        $start++;
        $end--;
    }
}    
 
/* Utility function to
   print an array */
function printArray(&$arr, $size)
{
for ($i = 0; $i < $size; $i++)
echo $arr[$i] . " ";
 
echo "
";
}
 
// Driver code
$arr = array(1, 2, 3, 4, 5, 6);
 
// To print original array
printArray($arr, 6);
 
// Function calling
rvereseArray($arr, 0, 5);
 
echo "Reversed array is" ."
";
 
// To print the Reversed array
printArray($arr, 6);
 

?>
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #php #Write #program #reverse #array #string
ADD COMMENT
Topic
Name
8+8 =