You’re not checking the value correctly. A single =
sign is the assignment operator. So when you write $variable = 0
you are setting the variable to 0
. This happens regardless of whether or not you’re in an if
statement.
So when you write if ( $testvalue = 0 ) {
you’re setting $testvalue
to 0, which is essentially the same as false, so the condition will fail.
To compare a value you need to use a comparison operator. You can see that link for more information, but essentially you need to use ==
to check if values are the same, or ===
to check if they’re the same and the type.
if ( 0 === $testvalue ) {
echo 'Before: ' . $testvalue;
} else {
echo 'Firing of Else statement but the value of $testvalue is ' . $testvalue;
}
Note that I’ve used a yoda condition. From the WordPress PHP Coding Standards:
When doing logical comparisons involving variables, always put the
variable on the right side and put constants, literals, or function
calls on the left side.