Php 在if语句中赋值?

Php 在if语句中赋值?,php,codeigniter,coding-style,if-statement,Php,Codeigniter,Coding Style,If Statement,当使用CodeIgniter时,我创建了很多类似这样的函数 function info($id){ $r = $this->db->select('id', 'name', 'age')->from('users')->where('id', $id)->get(); return ($r->num_rows() == 0 ? false : $r->result()); } 现在,当我使用这个函数时,我也使用这个函数来检查用户是否存在,并将其

当使用CodeIgniter时,我创建了很多类似这样的函数

function info($id){
  $r = $this->db->select('id', 'name', 'age')->from('users')->where('id', $id)->get();
  return ($r->num_rows() == 0 ? false : $r->result());
}
现在,当我使用这个函数时,我也使用这个函数来检查用户是否存在,并将其分配给一个变量

因此,它将被用作

if( ($user = $this->user->info($_GET['id'])) === false )
  die('User not found');

//now we can continue and $user contains the user info
我的问题是,这是个坏主意有什么原因吗

据我所知,这与

$user = $this->user->info($_GET['id']);
if( $user === false )
  die('User not found');

但在我看来,实际上更容易理解。

正如你所说,这两种方法是相同的。唯一的区别是清晰。我相信第二种方法比第一种更清楚。您应该与从事您的项目的其他开发人员讨论您的约定,并将标准设置为代码依据。一致性是最重要的

第一种方法的缺点是,很难判断您是否打算使用
=
而不仅仅是
=
。在第二种方法中,没有这种模糊性