cakephp:不理解类似于if的声明

时间:2012-03-02 05:21:45

标签: cakephp

我在cupcake论坛插件的forum_app_model.php中找到了以下代码:

/**
 * Validates two inputs against each other
 * @access public
 * @param array $data
 * @param string $confirmField
 * @return boolean
 */
public function isMatch($data, $confirmField) {
    $data = array_values($data);
    $var1 = $data[0];
    $var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : '';

   //== matches the values. Whereas === matches the values and the data type of the values
   //eg
    return ($var1 === $var2);
}

有人可以告诉我什么是$ var2 =(isset($ this-> data [$ this-> name] [$ confirmField]))? $ this-> data [$ this-> name] [$ confirmField]:'';在上面的功能?它看起来像一个if else stmt,但我不理解它。

谢谢。

2 个答案:

答案 0 :(得分:0)

这类似于:


if(isset($this->data[$this->name][$confirmField])) {
    $var2 = $this->data[$this->name][$confirmField];
}
else {
    $var2 = '';
}

请参阅:Ternary operator php

答案 1 :(得分:0)

您可以将此声明视为

if(isset($this->data[$this->name][$confirmField])) 
{
    $var2 = $this->data[$this->name][$confirmField] 
}else{
    $var2 = '';
}

感谢