Skip to content

Instantly share code, notes, and snippets.

@nickrouty
Created March 8, 2017 18:34
Show Gist options
  • Save nickrouty/e5fe0d2c571e74991391af52ddf18e92 to your computer and use it in GitHub Desktop.
Save nickrouty/e5fe0d2c571e74991391af52ddf18e92 to your computer and use it in GitHub Desktop.
Palindrome or not?
<?php
$string = "madam";
$array = str_split($string);
$reverse_array = array_reverse( $array, true );
$new_string = implode('', $reverse_array);
echo "<pre>";
var_dump($array, $reverse_array);
echo ( $string === $new_string) ? "Yes, {$string} is a palindrome." : "No, {$string} is not a palindrome.";
echo "</pre>";
// OR a simpler, less fun way ;)
$new_string = strrev( $string );
echo "<pre>";
echo ( $string === $new_string) ? "Yes, {$string} is a palindrome." : "No, {$string} is not a palindrome.";
echo "</pre>";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment