php - Define and check variable in if statement -


i wanting see if there simplified way of doing if statement checking multiple conditions, uses data same function.

if($class->function($id) && $class->function($id)['key'] > 0) {      //do something... } 

the issue here we're making 2 database calls retrieve same data. 1 obvious solution doing following:

$classvariable = $class->function($id); if($classvariable && $classvariable['key'] > 0) {     //do something... } 

my question is, can achieve same effect inside if statement? example(this not work):

if($classvariable = $class->function($id) && $classvariable['key'] > 0 {     //do something... } 

is possible, , there preferred way of doing this? seems me defining , using in same statement cleaner code...

as niet said, using parentheses () around $classvariable assignment , things should work.

(i wanted post answer rather comment question gets resolved, sorry niet)

edited op show answer in code...

if(($classvariable = $class->function($id)) && $classvariable['key'] > 0) {     //do something... } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -