gpt4 book ai didi

php - 将函数从 JS 转换为 PHP

转载 作者:行者123 更新时间:2023-11-28 13:54:15 27 4
gpt4 key购买 nike

我在 javascript 中有一个函数,它工作得很好,但现在我需要在 PHP 中使用相同的函数,它有非常奇怪的行为。在我看来,功能是相同的,但我还是错过了一些东西,找不到它。

borders、neighbors 是预定义的数组,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)];
}

最佳答案

可能是连接运算符(取决于您的函数返回字符串还是数字):

return $base . $table[strpos($neighbors[$direction][$type], $lastChar)];
-------------^

这里.用于PHP替换JS的+进行串联。

关于php - 将函数从 JS 转换为 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8991671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com