Categories
PHP

Palindrome in php

What is Palindrome?

A palindrome is a word, phrase, number, or sequence of characters which reads the same in either direction. The words A and I are the simplest and smallest palindromes. Few of the most commonly used are :-
RACECAR LEVEL CIVIC POP MADAM EYE NUN RADAR

The word palindrome is derived from the Greek palíndromos, meaning running back again (palín = AGAIN + drom–, drameîn = RUN).

Words like LIVE and STRAW (which read EVIL and WARTS backwards) are not themselves palindromes but the phrases LIVE EVIL and STRAW WARTS are. A palindrome is not necessarily a single word.

While creating palindrome, it is usually accepted that punctuation and word spacings are ignored, and so the famous MADAM, I’M ADAM is a valid palindrome.

Here are a few good ones:

    Do geese see God?
    Murder for a jar of red rum.
    Some men interpret nine memos.
    Never odd or even.

palindrome
A famous palindrome “Able was I ere I saw Elba” purportedly spoken by Napoleon, referring to his first sighting of Elba, the island where the British exiled him.

How to verify a word is palindrome ?

Find given string is palindrome or not in PHP using native strrev() function

<?php
$word = 'madam';  // declare a varibale
echo 'String: <b>' . $word . '</b>';
$reverse = strrev($word); // reverse the word
if ($word == $reverse) // compare if the original word is same as the reverse of it
    echo ' is a palindrome';
else
    echo ' is not a palindrome';
?>

Find given string is palindrome or not in PHP without using strrev() function

<?php
$mystring = 'madam'; // set the string
echo 'String: <b>' . $mystring . '</b>';

$myArray = array(); // initialize an array
$myArray = str_split($mystring); //split into array
$len = sizeof($myArray); // get the size of array
$newString = '';

for ( $i = $len; $i >= 0; $i-- ) {
    $newString .= $myArray[$i];
}

if ( $mystring == $newString ) {
    echo ' is a palindrome';
} else {
    echo ' is not a palindrome';
}
?>
'Coz sharing is caring
Categories
PHP

PHP Array with Numeric key or string type

I have a PHP array that has numeric keys as a string type.
But when I try and access them, PHP is giving me an undefined index error.

$a = (array)json_decode('{"1":1,"2":2}');
var_dump($a);
var_dump(isset($a[1]));
var_dump(isset($a["1"]));
var_dump($a[1]);
var_dump($a["1"]);

Output:

array (size=2)
    '1' => int 1
    '2' => int 2

boolean false

boolean false

ERROR: E_NOTICE: Undefined offset: 1
null

ERROR: E_NOTICE: Undefined offset: 1
null

Now the issue is “How do I access these values”?

Let’s start our environment, $obj equals the object notation of a decoded string:

php > $obj = json_decode('{"1":1,"2":2}');

php > print_r($obj);
stdClass Object
(
    [1] => 1
    [2] => 2
)

php > var_dump( $obj );
object(stdClass)#1 (2) {
  ["1"]=>
  int(1)
  ["2"]=>
  int(2)
}

If you want to access the strings, we know the following will fail:

php > echo $obj->1;

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in php shell code on line 1

Accessing the object variables
You can access it like so:

php > echo $obj->{1};
1

Which is the same as saying:

php > echo $obj->{'1'};
1

Accessing the array variables
The issue with arrays is that the following return blank, which is the issue with typecasting.

php > echo $obj[1];
php >

If you typecast it back, the object is once again accessible:

php > $obj = (object) $obj;
php > echo $obj->{1};
1

Here is a function which will automate the above for you:

function array_key($array, $key){
    $obj = (object) $array;
    return $obj->{$key};
}

Example usage:

php > $obj = (array) $obj;
php > echo array_key($obj, 1);
1

php > echo array_key($obj, 2);
2

If you want array, set the second parameter of json_decode to true.

$a = json_decode('{"1":1,"2":2}', true);

when you cast a std object to array, numeric string key doesn’t cast to number. Here is an example.

$obj = new stdClass;
$obj->{'1'} = 1;
$arr = (array) $obj;
var_dump($arr);
var_dump(isset($arr[1]));
'Coz sharing is caring