The problem is with the original code, which is certainly not the same as the question, since the question code works perfectly the way it is .
It should probably be with ===
in the condition (but of course it may be something else wrong in the original code).
See in the Ideone an example with the code of the question (taking the value that would be obtained by post
) with the string "1"
in variable $campo1
.
However, if you change the condition to ===
it will already give you an error, see here on Ideone .
As the @rray suggested in the comment, see var_dump
of variables (in this case sent by post
through input
type="text"
, just as it is in the question code):
string '1' (length=1)
int 1
This happens because the input
field of type="text"
always sends a string (even if it is a number), while the declaration of the $valor1
variable is as an integer, and the ===
comparator requires that the values are identical , including the type.
PHP manual :
$a === $b // Idêntico Verdadeiro (TRUE) se $a é igual a $b, e eles são do mesmo tipo.
Detail: The accepted response would work even with the ===
comparator, because it has transformed the string to integer with (int)
, which is unnecessary in this case because it is using ==
.