What does an exclamation mark before a function really mean in PHP
The !
preceding the function is the same as...
if (stripos($haystack, $needle) == FALSE) {}
It's the same because it is a ==
comparison which doesn't check types.
It's called the negation unary operator. It flips the Boolean value (coercing to Boolean if need be) of a value.
For example...
! 0; // True
! 1 ; // False
! ''; // True
! true; // False
!! 0 // False
The !!
trick is handy in languages without a (bool)
cast. By flipping a value twice, you get the Boolean version of its original value.
What does the exclamation mark do before the function?
JavaScript syntax 101: here is a function declaration:
function foo() {}
Note that there’s no semicolon; this is just a function declaration. You would need an invocation, foo()
, to actually run the function.
Now, when we add the seemingly innocuous exclamation mark: !function foo() {}
it turns it into an expression. It is now a function expression.
The !
alone doesn’t invoke the function, of course, but we can now put ()
at the end: !function foo() {}()
, which has higher precedence than !
and instantly calls the function.
function foo() {}()
would be a syntax error because you can’t put arguments (()
) right after a function declaration.
So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this:
(function(){})();
Lastly, !
makes the expression return a boolean based on the return value of the function. Usually, an immediately invoked function expression (IIFE) doesn’t explicitly return anything, so its return value will be undefined
, which leaves us with !undefined
which is true
. This boolean isn’t used.
Exclamation mark in front of variable - clarification needed
if(! $a)
is the same as if($a == false)
. Also, one should take into account that type conversion takes place when using ==
operator.
For more details, have a look into "Loose comparisons with ==" section here. From there it follows, that for strings "0" and "" are equal to FALSE ( "0"==false
is TRUE and ""==false
is TRUE, too).
Regarding posted examples:
Example 1
It will work, but you should note, that both "0" and "" are 'empty' strings.
Example 2
It will work
What does an exclamation mark before a variable mean in JavaScript
!
is the logical not operator in JavaScript.
Formally
!expression
is read as:
- Take
expression
and evaluate it. In your case that'svariable.onsubmit
- Case the result of that evaluation and convert it to a boolean. In your case since
onsubmit
is likely a function, it means - if the function is null or undefined - return false, otherwise return true. - If that evaluation is true, return false. Otherwise return true.
In your case
In your case !variable.onsubmit
means return true if there isn't a function defined (and thus is falsy), otherwise return false (since there is a function defined).
Simply put - !variable
means take the truth value of variable
and negate it.
Thus, if (!variable) {
will enter the if
clause if variable is false
(or coerces to false)
In total
if (!variable.onsubmit || (variable.onsubmit() != false)) {
Means - check if variable.onsubmit
is defined and truthy (thus true), then it checks if calling onsubmit
returns a result that coerces to true. In a short line it checks if there is no onsubmit
or it returns true.
Next time, how do I find this myself?
- MDN has a list of operators here.
- The language specification specifies such operators, though being the official specification it does contain some jargon which might be hard to understand.
Why does an exclamation mark before a variable return 'true' if the variable value is zero?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Logic used in if statement using Exclamation Mark beside function
filter_var
returns the input value $ip
if it is valid. A valid IPV6 address is "truthy" (it is a non-empty string). The !
operator applied to a truthy value produces false
.
So if $ip
is valid, then !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false
.
Remember that the !
has higher precedence than the ===
. That's what makes the two code samples in your question different.
filter_var
returns false
if the test fails, so another perfectly good (and perhaps clearer) way of testing this would be:
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
// $ip is valid
}
The second piece of code effectively does the same thing (checking the result of filter_var
is false
), although it does so in a less explicit way.
Related Topics
$_Server['Remote_Port'] Not Giving Value
PHP $_Server['Php_Self'] to Include Query String
Fatal Error: Class 'Xsltprocessor' Not Found
Detect Retina (Hd) Display on the Server Side
Convert Hex Code into Readable String in PHP
Count All Child Nodes of Hierarchical Data in a Table
How to Resolve the Error: SQL Authentication Method Unknown in Laravel-Mysql
Php: 'Or' Statement on Instruction Fail: How to Throw a New Exception
Is This Mail() Function Safe from Header Injection
How to Convert a Single Array into a Multidimensional Array in PHP
Import Class Conditionally with the Keyword 'Use'
Linkify Regex Function PHP Daring Fireball Method
How to Use Multiple PHP Header Content Types on the Same Page? Is This Possible
Selecting Rows Where a Field Is Null Using PHP Pdo Prepared Statements and MySQL