I have a function in javascript and it works just fine, but now I need same function in PHP and it has very strange behavior. It seems to me that functions are identical, still I missed something and cannot find it.
borders, neighbors are pre-defined arrays and base is predefined string.
Java Script
function calculateAdjacent(sourceBase, direction) {
sourceBase = sourceBase.toLowerCase();
var lastChar = sourceBase.charAt(sourceBase.length-1);
var type = (sourceBase.length % 2) ? 'odd' : 'even';
var base = sourceBase.substring(0,sourceBase.length-1);
if (BORDERS[direction][type].indexOf(lastChar)!=-1)
{
base = calculateAdjacent(base, direction);
}
return base + TABLE[NEIGHBORS[direction][type].indexOf(lastChar)];
}
PHP
($table, $neighbors, $borders are defined, they take a lot of space but I can but them in.)
function calculateAdjacent($sourceBase, $direction)
{
$sourceBase = strtolower($sourceBase);
$lastChar = $sourceBase[strlen($sourceBase) - 1];
if (strlen($sourceBase) % 2)
{
$type = "odd";
}
else
{
$type = "even";
}
$base = substr($sourceBase, 0, strlen($sourceBase) - 1);
if (strpos($borders[$direction][$type], $lastChar) === false)
{
$base = calculateAdjacent($base, $direction);
}
// Problem in this line, need to fix '+' to '.'
return $base + $table[strpos($neighbors[$direction][$type], $lastChar)];
}
Concatenation operator probably (depending whether your function returns string or number):
Here
.is used for PHP replacing JS’s+for concatenation.