我在javascript中有一个函数,它工作正常,但现在我需要PHP中的相同功能,它有非常奇怪的行为。在我看来,功能是相同的,但我仍然遗漏了一些东西而无法找到它。
边框,邻居是预定义的数组,base是预定义的字符串。
Java脚本
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被定义,它们占用了很多空间,但我可以,但它们可以。)
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)];
}
答案 0 :(得分:5)
可能是连接运算符(取决于函数是返回字符串还是数字):
return $base . $table[strpos($neighbors[$direction][$type], $lastChar)];
-------------^
此处.
用于替换JS的+
以进行连接。
答案 1 :(得分:0)
$borders
变量未定义(或未定义)。
修改: $neighbors
。
答案 2 :(得分:0)
另外,你在JS中使用三元运算符,而不是PHP,只是好奇为什么?
if (strlen($sourceBase) % 2)
{
$type = "odd";
}
else
{
$type = "even";
}
可以在php中
$type = strlen($sourceBase) % 2 ? "odd" : "even";