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.
You probably have ===
in condition (but of course it may be something else wrong in the original code).
See on Ideone an example with the question code (taking the value that would be obtained by post
) with the string "1"
in the variable $campo1
.
However, if you change the condition to ===
It’ll be a mistake, look here at Ideone.
As @rray suggested in the comment, see var_dump
of the variables (in this case sent by post
through a input
of type="text"
, exactly as it is in the question code):
string '1' (length=1)
int 1
This happens because the field input
of type="text"
always send a string (even if it is a number), while the variable declaration $valor1
is as a whole, and the comparator ===
requires that values be identical, including the type.
Of PHP manual:
$a === $b // Idêntico Verdadeiro (TRUE) se $a é igual a $b, e eles são do mesmo tipo.
Detail: The accepted answer would work even with the comparator ===
, because it turned the string into integer with (int)
, which is unnecessary in the case, for being using ==
.
The two variables are from the same datatype?
– Mairon de Souza Pereira
I believe so. I am comparing only whole numbers.
– Lucas A.
checked the point and comma on line 2?
– user38006
In
$campo1 = $_POST['um']
missing a;
in the end.– gustavox
I just forgot to put it in, but the original code is
;
– Lucas A.
The problem does not seem to be the code but the values ...
var_dump()
in doubt!– rray
In its original code the condition would not be with
===
? Because in case the1
of$campo1
isstring
and that of$valor1
is whole... here for me your code works as it is, I think because you are with==
...– gustavox