为什么我的PHP函数返回1或nothing而不是true或false?
我用PHP编写了这个回文函数为什么我的PHP函数返回1或nothing而不是true或false?,php,Php,我用PHP编写了这个回文函数 <?php // example code function isPalindrome($str){ // eliminate special chars $str = preg_replace('/[^a-z0-9]+/i', '', $str); // all to lowercase $str = strtolower($str); // reverse string $strArr = str_sp
<?php
// example code
function isPalindrome($str){
// eliminate special chars
$str = preg_replace('/[^a-z0-9]+/i', '', $str);
// all to lowercase
$str = strtolower($str);
// reverse string
$strArr = str_split($str);
$strArr = array_reverse($strArr);
$reversed_str = join('',$strArr);
//Test
//echo $str . ' | ' . $reversed_str;
// compare
return $str === $reversed_str;
}
echo isPalindrome("A nut for a jar of tuna"); // returns 1
我希望它返回true或false。我的错误在哪里?您的函数不会返回
1
或什么都不返回,是echo
决定以这种方式呈现结果
试一试
为了获得更易于调试的输出这是一种过度工程化的形式,我希望它能像我预期的那样工作(它在JavaScript中工作)。谢谢你的回答。
echo isPalindrome("A nut for a jar of fish"); // returns no output (in https://www.tehplayground.com)
var_dump(isPalindrome("A nut for a jar of tuna"));
if (isPalindrome("A nut for a jar of tuna") === true) {
echo 'true';
} else {
echo 'false';
}