Skip to content

Instantly share code, notes, and snippets.

@LarrySul
Last active September 8, 2021 13:19
Show Gist options
  • Save LarrySul/160291881601eb76d7bfbfe69d7081c8 to your computer and use it in GitHub Desktop.
Save LarrySul/160291881601eb76d7bfbfe69d7081c8 to your computer and use it in GitHub Desktop.
Write a function that can be used to reverse a string in php
<?php
function reverse_string($str) : string
{
if(!is_string($str)) return "not a string";
$lengthString = strlen($str);
$data = [];
for($i = ($lengthString -1); $i >=0; $i--){
$data[] = $str[$i];
}
return implode("", $data);
}
print_r(reverse_string("John Peich"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment