Register now or log in to join your professional community.
If your question is to reverse a string with out using any built in function in PHP, then i have created a class that would reverse the string for you. If your need is to reverse a string with out using PHP, then javascript is the answer. First i will provide with the PHP class.
class Reverse_String{
private $i;
private $str;
private $output;
public function __construct($str){
$this->str = $str;
}
private function getStringLength(){
for($i=0;;$i++){
if(!$this->str[$i]){
$this->i = $i-1;
throw new Exception('My Error.Just wanted to get the string length.');
}
}
}
public function reverseString(){
try{
$this->getStringLength();
} catch (Exception $e) {
for($j=$this->i;$j>=0;$j--){
$this->output .= $this->str[$j];
}
return $this->output;
}
}
}
$rev = new Reverse_String('String to be reversed');
$str = $rev->reverseString();
echo $str;
Here we are looping the string with an infinite loop in getStringLength() function, and we create a custom exception. In reverseString() function we catch that exception. Now we have the string length. Now simply a reverse looping will do our job. Hope my answer is suited to your question.
If you need a javascript alternative, then i have answered it. please go to this link :
http://www.bayt.com/en/specialties/q/26811/26811/
use simple mechanism as ::
Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.