It seems that I can't use a variable defined in an if statement in a later part of the same statement. Is there a way around this? Here's the statement:
if ($cat = getCat($item) &&
($masterCat= getVC($cat) || $masterCat= getTC($cat))) {
echo "success";
} else {
echo "fail";
}
As you can see, $cat is defined in the first part. If that part evaluates False then the other parts won't run. My problem is that I get
Undefined variable: cat
even though getCat is called and $cat defined before the RHS of the && operator is evaluated.
Building this code I learned about short circuit evaluation and Truthy values but now I'm stuck trying to "one line" it. I can nest if statements but was wondering as there are three possible points of failure if there's a way to do this where the "fail" part is only defined once (it's bigger in the real code, not just echo "fail";)
functions below
function getCat($pItem) {
//looks up $pItem and returns a category or null if $pItem doesn't exist
}
function getVC($pCat) {
//looks up $pCat quickly, returns master category or null if not in quick lookup table
}
function getTC($pCat) {
//looks up $pCat thoroughly,
//returns master category OR
//returns null if $pCat has expired (shouldn't happen but prevents crashing if it ever does)
}