I am wanting to see if there is a simplified way of doing an if
statement that is checking multiple conditions, where it uses data from the same function.
if($class->function($id) && $class->function($id)['key'] > 0)
{
//Do something...
}
The issue here is we're making 2 database calls that retrieve the same data. One obvious solution is doing the following:
$classVariable = $class->function($id);
if($classVariable && $classVariable['key'] > 0)
{
//Do something...
}
My question specifically is, can I achieve the same effect all inside the if
statement? For example(this will not work):
if($classVariable = $class->function($id) && $classVariable['key'] > 0
{
//Do something...
}
Is this possible, and is there a preferred way of doing this? It seems to me that defining and using it in the same statement would be cleaner code...
As Niet said, using parentheses () around the $classVariable assignment and things should work.
(I just wanted to post this as an answer rather than a comment so the question gets resolved, sorry Niet)
Edited by OP to show answer in code...
if(($classVariable = $class->function($id)) && $classVariable['key'] > 0)
{
//Do something...
}