在php中返回或条件的值

在php中返回或条件的值,php,return,Php,Return,我有两个函数将根据条件运行。 代码如下 $contact=($this->function() or $this->function1()) public function() { some codes return contact; } public function1() { some codes return contact; } 它在$contact中返回bool true或false。我想返回值。怎么办 如果我这样给予 $contact=$this-

我有两个函数将根据条件运行。 代码如下

$contact=($this->function() or $this->function1())

public function()
{
 some codes
return contact;
}

 public function1()
  {
 some codes
 return contact;
   }
它在$contact中返回bool true或false。我想返回值。怎么办

如果我这样给予

$contact=$this->function() or $this->function1()
$contact=$this->function() ?: $this->function1();

如果函数()为false,则不会检查函数1()。

如果要检查两个函数,则测试两个函数并将其结果保存在单独的变量中,然后在if比较中使用这两个变量,因为您使用的是布尔运算符或

$this->function() or $this->function1()
是布尔值。 在PHP5.3中,可以像这样使用三元运算符

$contact=$this->function() or $this->function1()
$contact=$this->function() ?: $this->function1();
如果是早期版本

if (!($contact=$this->function() )) $contact=$this->function1();

但是,总的来说,我认为你必须重新审视你的功能并改变它们的流程。可能必须从这两个函数中选择一个,并在此函数中做出决定。

您不能返回值并使用
运算符
一种方法是设置变量并在函数中对其赋值,如下所示:

    $ret = "";
function ()
{

    $this->ret = "foo";
    return contact;
}

function1() {
    $this->ret = "Bar";
    return contact;
}

$a = function() or function1();
unset($a);
$newRet = $ret;
有关更多详细信息,请参见此处:
应该是

$contact = $this->A() or $contact = $this->B();


那么应该返回哪个值呢?你想在
$contact
中得到什么?或者像
|
一样使用什么?并
返回$contact?-1。提供具体的代码,而不是不编译的代码。