我正在尝试使用if语句来使用bool值,但它不起作用。顶部是我正在使用的功能,底部是if语句。当我将if语句更改为false时,我得到结果,但我需要true和false bools。任何提示
public function find($key) {
$this->find_helper($key, $this->root);
}
public function find_helper($key, $current){
while ($current){
if($current->data == $key){
echo " current";
return true;
}
else if ($key < $current->data){
$current= $current->leftChild;
//echo " left ";
}
else {
$current=$current->rightChild;
//echo " right ";
}
}
return false;
}
if($BST->find($randomNumber)){//how do I get this to return a true value?
echo " same ";
}
答案 0 :(得分:7)
您从find_helper()
返回,但不是从find()
返回。如果没有return
(见下文),则会调用find_helper()
方法,但无论该方法返回的是丢弃。因此,您的find()
方法最终返回 值(无论如何,PHP转换为null)。
public function find($key) {
return $this->find_helper($key, $this->root);
}
答案 1 :(得分:0)
使用三元运算符
public function find($key) {
return ($this->find_helper($key, $this->root)) ? true : false;
}