3

This is the code I have so far. I just started learning PHP today and I'm not sure why my code isn't working.

<?php

function backwards($input)
{
    $str = $input;
    $revStr = "";
    $str = explode(",",$str);
    for($x = 0; $x < strlen($str); $x++){
        $revStr .= strrev($str[$x]);
    }
    return $revStr;
}

Any help would be greatly appreciated!

edit: Here is an example input

Php,Arrays,Mysql

here is what i would like the output to be:

phP,syarrA,lqsyM

edit2:

Figured it out. Made a few minor edits. Not sure if it's the most efficient code but it works.

<?php

function backwards($input)
{
    $str = $input;
    $revStr = "";
    $str = explode(",",$str);
    for($x = 0; $x < count($str); $x++){
        if($x == count($str) - 1){
            $revStr .= strrev($str[$x]);
        }
        else{
            $revStr .= strrev($str[$x]) . ",";
        }
    }
    return $revStr;
}
mario
  • 144,265
  • 20
  • 237
  • 291
Logan Kroeze
  • 85
  • 1
  • 10

5 Answers5

2

Edit, I see you now included an example.
Use array_map and strrev to reverse the words.
Use explode and Implode to make the words array and back to string.

echo implode(",", array_map(function($part){ return strrev($part);},explode(",", $str)));

https://3v4l.org/tr7d6


I believe you should use array_reverse on the exploded string.
Then you can implode it back to a string.
$str = "apple,orange,tomato";

$arr = explode(",", $str);
$arr =array_reverse($arr);
echo implode(",", $arr); //tomato,orange,apple

https://3v4l.org/Hvd9m

Or, slightly messier but compact:

echo implode(",", array_reverse(explode(",", $str))); 

https://3v4l.org/LTu90

Andreas
  • 23,610
  • 6
  • 30
  • 62
1

You are checking the string length of an array inside of your for loop. Also, a custom function is unnecessary, PHP provides us with strrev().

But if you would like to use your own:

function backwards($input)
{
    $str = $input;
    $revStr = "";
    $str = explode(",",$str);
    for($x = 0; $x < count($str); $x++){
        $revStr .= strrev($str[$x]);
    }
    return $revStr;
}

$string = "Php,Arrays,Mysql";
echo backwards($string);

Hope this helps,

Community
  • 1
  • 1
Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
1

You need to use count instead of strlen and also need to take reversed string into array and then convert it into string.

You can try this code :

<?php

function backwards($input)
{
    $str = $input;
    $revStr = array();
    $str = explode(",", $str);
    for ($x = 0; $x < count($str); $x++) {
        $revStr[] = strrev($str[$x]);
    }
    return implode(',', $revStr);
}
$str = "Php,Arrays,Mysql";
var_dump(backwards($str));
Ravi Sachaniya
  • 1,641
  • 18
  • 20
1

use $str[0] in for loop syntax.

like this => for($x = 0; $x < strlen($str[0]); $x++)
Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
Vikal Singh
  • 84
  • 1
  • 2
  • 10
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.[Read this](https://stackoverflow.com/help/how-to-answer). – Shanteshwar Inde Mar 13 '19 at 04:50
1

Just some modification and you got your desired output. You can use explode and implode function of php with array_push to get your result:

<?php

$string = "Php,Arrays,Mysql";

function backwards($input)
{
    $str = $input;
    $revStr = array();
    $str = explode(",", $str);
    for ($x = 0; $x < count($str); $x++) {
        array_push($revStr, strrev($str[$x]));
    }
    return implode(',', $revStr);
}

$result =backwards($string);

echo $result;

?>

You can check the demo here

Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40