Wednesday 22 May 2013

Convert 123456789 string to 9,8,7,6,5,4,3,2,1 without using string function

Hello every one.

I have another chalange in php. can you convert "123456789" string to "9,8,7,6,5,4,3,2,1" this string format without using string function.

first you tell this is not possible, but this is possible using array functions. See below code.

<?php

error_reporting(0);
$str="123456789";
$a=array();
for($i=0;$i<1000;$i++)
{
if($str[$i]!="")
{
array_push($a,$str[$i]);
}
else
{
break;
}
}
$exp=array_reverse($a);
$final="";
for($i=0;$i<count($exp);$i++)
{
if($i!=count($exp)-1)
{
$final.=$exp[$i].",";
}
else
{
$final.=$exp[$i];
}
}
echo $final."<br>";

?>

Using this code you will get "9,8,7,6,5,4,3,2,1".