How does PHP read if statements?
I have the following if statements in this order
if ( $number_of_figures_in_email < 6) {
-- cut: gives false
}
if($number_of_emails > 0) {
-- cut: gives false
}
if ( $number_of_emails == 0) {
-- cut: gives true
}
The code behaves randomly. It sometimes goes to the third if clause and gives me a success, while sometimes to the one of the first two if clauses when the input variables are constant.
This suggests me that I cannot code only with if statements.
It doesn’t “behave randomly”, it does what you tell it to do:
All three
ifsare independent of each other. If$a,$band$care alltrue, it’ll do A, B and C. If only$aand$care true, it’ll do A and C, and so on.If you’re looking for more “interdependent” conditions, use
if..elseor nestedifs:In the above only one action will get executed.
In the above both B and C might get done, but only if
$ais false.This, BTW, is pretty universal and not at all PHP specific.