I have built a view system, works great but i was wondering if anyone can see a better way to optimise this piece of code as phpStorm does not like the fact the if/elseif have empty bodies so can anyone see a really easy way to make it more optimised. e.g. less if statements, simpler etc. Thanks
if (startHelper::isLoggedIn() && $getItemById['user_pin'] === $_SESSION['user']['pin']) {
//'view does not count for own';
} elseif($this->start->alreadyViewedToday($view)) {
//'already viewed';
} else {
$this->start->insertView($view); // 2 = item // 1 == page
}
So, You can do it all with a single if, no else clauses.
You will need: (): To wrap terms ||: the “OR” evaluator !: the “NOT” evaluator.
If your current structure is
if(a) { X }
elseif(b) { Y }
else {
Z
}
and so it reads “If a, X, else if b, Y else Z”.
How do you reverse that, so that Z is in the X position?
(Or perhaps it would be easier to figure out “What must be true to get to Z?”)
“If A, X, else if B, Y, else Z”
for Z to happen, both A and B must be false. Which would be written logically either as “Not A And Not B” or as “Not (A or B)” (by De Morgan’s Law)
Note that I didnt change anything inside of A or B.
I’m glad you got there
The warning is generated because your internal line (the red squiggly bit + the ending) reads as A && B || C. This is potentially ambiguous language, because “A and B or C” said out loud could mean “(A and B) or C” or “A and (B or C)” depending on where you put your inflection. Code can’t use inflection, so it uses/requires parenthesis
Wow. Thanks for that explanation. Helpful and also thankyou goodness for being a student and getting PHPStorm free to help me learn with my mistakes. It’s an amazing application