0
Could someone explain to me the use of ! at the beginning of the code?
$number = 28;
$resultado = !($number % 2) ? "O número é par" : "O número é ímpar";
echo $resultado;
0
Could someone explain to me the use of ! at the beginning of the code?
$number = 28;
$resultado = !($number % 2) ? "O número é par" : "O número é ímpar";
echo $resultado;
9
In this code the use of the negation operator is an error or at least abuse. Even if it produces the expected result does not make sense and only does not give error of execution because PHP is a weak typing language.
The negation operator takes a boolean value, therefore false or true and reverses its value.
The result of $number % 2
is a number and not a boolean so it is not correct to apply the negation operator to it. Works and in some cases can produce what you want, but is confusing.
In PHP any value 0 (or equivalent to 0) is always considered false and any other value is considered true, so the language makes an automatic coercion of type following this rule (documentation).
This is a case that even works because the result will always be 0 or 1 so coercion works well. So it would be more explicit what is the condition you want to be false or true:
$number % 2 == 0 ? "O número é par" : "O número é ímpar";
This way the code is very clear that you want it to be true when the result is 0, and we know that the rest of a division by 2 is always 0 when the number per pair, if 1 (therefore different from 0) is odd.
And if you really want to use the result as boolean you don’t have to do the negation:
$number % 2 ? "O número é ímpar" : "O número é par";
I put in the Github for future reference.
The cases that negation is necessary and useful are rare, unless it comes from a function that returns a boolean, but by an expression you can always build it differently to avoid the operator, even if it doesn’t always pay off. Readability to demonstrate intent is what comes first, make sure denial will help you better understand. This is not the case in the question.
Now I understood, thank you Maniero, helped a lot. I am studying and I could not find sense in the answer for anything. Now everything became clearer.
@Aline just to confirm, you chose the answer that helped you the most? You can choose any one, but you can’t choose more than one. I noticed that you chose one and then changed to another. The chosen one is always the last one you choose. It’s like a Radion button, when you choose one turns the other off. Many people switch without noticing. It is important to keep chosen the one that helped you mias, if it was the other ok, but if it was accident, mense in changing.
-6
In PHP, 1
and 0
are values truthy
and falsy
, they can be used as logical conditions, although their types are different from boolean.
In your example you have $number % 2
. As the value of $number
is 28
, the rest of this division will be 0
, that is, it is falsy
, equivalent to false
in that case of use.
Just in doing $resultado = false ? "O número é par" : "O número é ímpar"
, $resultado
would be receiving O número é ímpar
, which is not true, for 28 is par.
To solve this problem, you could reverse your ternary expression, or else reverse your result.
That is to say,
$resultado = $number % 2 ? "O número é ímpar" : "O número é par";
Or else
$resultado = !($number % 2) ? "O número é par" : "O número é ímpar";
In the latter case, the result of 28 % 2
; 0
, which corresponds to a value falsy
, is denied, becoming true
, soon $resultado
receives O número é par
, which is exactly what you want.
It is not true that these boolean values are alias for the numbers 0 and 1.
If you do echo true
, PHP prints 1
, nay?
And why is it done 2 == true
results in true
? So true
is also alias for 2? And so true
is alias for any value other than 0 or a form of 0? Documentation that does not speak in alias, speaks in conversion of values. https://i.stack.Imgur.com/Vxr9w.jpg And PHP is not JS either, so it does not use the terms and definitions placed in the answer after editing, there are different implications.
If you do !($number % 3) ? "O número é múltiplo de 3" : "O número não é múltiplo de 3";
drops the entire premise that 0 and 1 are special values to be used as logical conditions.
In the PHP manual specifies that when converting to boolean, the following values are considered FALSE the boolean itself FALSE, the integers 0 and -0 (zero), the floating points 0.0 and -0.0 (zero), an empty string and the string "0", an array without elements, the special type NULL (including undefined variables), Simplexml objects created from empty tags
Any other value is considered TRUE (including any resource and NAN).
Browser other questions tagged php operators
You are not signed in. Login or sign up in order to post.
! It is negation, example ! false is true, and ! true is false.
!$a
True if $a is not true.– Boi Programador
In theory, the code without the ! should return true "The number is even" but it returns as false when I shoot the !... Would you please explain to me why?
– Alline Oliveira
Operator ! and as a given information true, you force it to be false..
– Leandro Lobo