return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole) == 0);
What exactly does the || mean in this statement? Can someone put this in english for me.
I promise I've googled this but I guess I don't know what to google for because I can't find anything.
thanks:)
It is the OR logicial operator.
http://www.php.net/manual/en/language.operators.logical.php
Googling symbols is always hard. Don't worry: || means or in the statement. Don't confuse it for Xor which is slightly different:
or or || is meant as A or B or A + B
xor is meant as A or B, not both
References:
Logical operator
This is an OR operator. It is true if any of it's 'parameters' is true.
|| means or.
It's a logical or, so it's true if at least one of the terms is true, false otherwise.
Related
I'm learning validation stuff, and I just can't understand this:
if (strpos($value, "#") === false) { echo "Validation failed." }
What's the difference between === and ==? and why can't we use == instead and also why is it === false? does false means that # is not in the $value or it means 0 ?
The Equality Operator ==
A == B checks whether A and B are equal to each other, but not whether they are the same data type.
A pertinent example: 0 == false is true
The Identity Operator ===
A === B checks whether A and B are equal to each other also the same data type.
A pertinent example: 0 === false is false
Application Here
Applying this to your case, if the # was found as the first character of the string, strpos($value,"#") would return 0. If it is not found at all, it would return false.
So to avoid confusing these two situations, the test must use === rather than ==.
Useful references:
http://php.net/manual/en/function.strpos.php
http://php.net/manual/en/language.operators.comparison.php
I've assumed this is php, but the equality and identity operators are common to many programming languages.
I am trying to set up a simple php/mysql database on my website and the example has a line that says:
if (!$searchtype || !?searchterm) {
What are the ||??? I tried to copy and paste them but they cause syntax errors. Noob question I know, but would love an answer nonetheless!
its ||(OR) logical operator
$a || $b Or TRUE if either/both $a or $b is TRUE.
and it should be
if (!$searchtype || !searchterm) {
as in op this Comment you can write this by
I think the code actually is :
if (!$searchtype || !$searchterm) {
|| means OR operator, meaning conditions is satisfied if either of the condition is true.
Its an OR logical operator for handling conditions:
Ex :
$a || $b
Or TRUE if either $a or $b is TRUE.
See : http://php.net/manual/en/language.operators.logical.php
for more details
I was wondering if there are any disadvantages of using words (e.g. AND, OR) instead of their code equivalents (&&, ||) for comparison? Besides the later being a compatible syntax with many other programming languages, is there any other reason for choosing them?
AND is not the same like &&
for example:
<?php $a && $b || $c; ?>
is not the same like
<?php $a AND $b || $c; ?>
the first thing is
(a and b) or c
the second
a and (b or c)
because || has got a higher priority than and, but less than &&
For more information check out PHP Logical Operators and Operator Precedence
An unanticipated disadvantage comes when used with the = operator.
$result = false || true; # true, $result is true
/* Translated to result = (false || true) */
and
$result = false or true; # true, $result is false
/* Translated to (result = false) or true */
The PHP manual (in Logical Operators) talks about what you ask in your question:
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)
So the difference is in the precedence, not the logical meaning of each single operator.
In your example: (x && y || z) and (x AND y OR z) you won't see any difference between the two expressions.
They do the same thing, but the && and || operators have higher precedence than AND and OR.
Basically I think this can become confusing so if you just stick to one notation and not mix them, you'll be fine and your code will remain readable.
I currently have the following line of code
elseif($_POST['aspam'] != 'fire'){
print "Is ice really hotter than fire?";
}
Is there any sort of OR function within PHP? as if to say if...
$_POST['aspam'] != 'fire' OR !='Fire'
OR alternatively make my value not case sensitive?
Hopefully this makes sense...
The || or or (lowercase) operator.
elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire'){
print "Is ice really hotter than fire?";
}
Sure.
$_POST['aspam'] != 'fire' or $_POST['aspam'] !='Fire'
Just remember that each condition is separate. Saying or != 'Fire' doesn't automatically interpret it as or $_POST['aspam'] != 'Fire'.
They're called logical operators.
To compare the lowercase:
strtolower($_POST['aspam'] != 'fire'
You can do two conditions like this:
if($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')
If I were you in this case, I would do:
if(strtolower($_POST['aspam']) != 'fire')
A PHP OR is created with ||, AND created with &&, etc. So your code example would look like:
if ( ($_POST['aspam'] != 'fire') || ($_POST['aspam'] != 'Fire') )
However in your case it would be better to:
if (strtolower($_POST['aspam']) != 'fire')
There are different logical operators in PHP.
Use for "OR" two pipes: ||
$_POST['aspam'] != 'fire' || !='Fire'
Here's a link with all operators:
http://www.w3schools.com/PHP/php_operators.asp
Yes.
if (first condition || second condition){
your code
}
The OR is represented by 2 pipes - ||
Something more:
You can also have AND:
if(first condition && second condition){
Your code...
}
So and is represented by &&
This is the logical OR
$_POST['aspam'] != 'fire' || !='Fire'
and this is the case-insensitive (ToLower function)
strtolower($_POST['aspam']) != 'fire'
Use strtolower($_POST['aspam'] )!='fire'
If you want to make the checking of your variable case insensitive, you can use below code
if(strtolower($_POST['aspam'])!='fire')
echo "this is matching";
OR alternatively make my value not
case sensitive?
if (strtolower($_POST['aspam']) != 'fire'){
}
Yes, this is possible. Try this:
elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')
You could use case insensitive string comparison:
if (strcasecmp($_POST['aspam'], 'fire') !== 0) {
print "Is ice really hotter than fire?";
}
Or a list:
if (!in_array($_POST['aspam'], array('Fire','fire')) {
...
The shortest option here would probably be stristr:
if (stristr($_POST["aspam"], "FIRE")) {
It does a case-insensitive search. To make it a fixed-length match you might however need strcasecmp or strncasecmp in your case. (However I find that less readable, and doesn't look necessary in your case.)
I have a codebase where developers decided to use AND and OR instead of && and ||.
I know that there is a difference in operators' precedence (&& goes before and), but with the given framework (PrestaShop to be precise) it is clearly not a reason.
Which version are you using? Is and more readable than &&? Or is there no difference?
If you use AND and OR, you'll eventually get tripped up by something like this:
$this_one = true;
$that = false;
$truthiness = $this_one and $that;
Want to guess what $truthiness equals?
If you said false... bzzzt, sorry, wrong!
$truthiness above has the value true. Why? = has a higher precedence than and. The addition of parentheses to show the implicit order makes this clearer:
($truthiness = $this_one) and $that
If you used && instead of and in the first code example, it would work as expected and be false.
As discussed in the comments below, this also works to get the correct value, as parentheses have higher precedence than =:
$truthiness = ($this_one and $that)
Depending on how it's being used, it might be necessary and even handy.
http://php.net/manual/en/language.operators.logical.php
// "||" has a greater precedence than "or"
// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;
But in most cases it seems like more of a developer taste thing, like every occurrence of this that I've seen in CodeIgniter framework like #Sarfraz has mentioned.
Since and has lower precedence than = you can use it in condition assignment:
if ($var = true && false) // Compare true with false and assign to $var
if ($var = true and false) // Assign true to $var and compare $var to false
For safety, I always parenthesise my comparisons and space them out. That way, I don't have to rely on operator precedence:
if(
((i==0) && (b==2))
||
((c==3) && !(f==5))
)
Precedence differs between && and and (&& has higher precedence than and), something that causes confusion when combined with a ternary operator. For instance,
$predA && $predB ? "foo" : "bar"
will return a string whereas
$predA and $predB ? "foo" : "bar"
will return a boolean.
Let me explain the difference between βandβ - β&&β - "&".
"&&" and "and" both are logical AND operations and they do the same thing, but the operator precedence is different.
The precedence (priority) of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator.
Mixing them together in single operation, could give you unexpected results in some cases
I recommend always using &&, but that's your choice.
On the other hand "&" is a bitwise AND operation. It's used for the evaluation and manipulation of specific bits within the integer value.
Example if you do (14 & 7) the result would be 6.
7 = 0111
14 = 1110
------------
= 0110 == 6
which version are you using?
If the coding standards for the particular codebase I am writing code for specifies which operator should be used, I'll definitely use that. If not, and the code dictates which should be used (not often, can be easily worked around) then I'll use that. Otherwise, probably &&.
Is 'and' more readable than '&&'?
Is it more readable to you. The answer is yes and no depending on many factors including the code around the operator and indeed the person reading it!
|| there is ~ difference?
Yes. See logical operators for || and bitwise operators for ~.
Another nice example using if statements without = assignment operations.
if (true || true && false); // is the same as:
if (true || (true && false)); // TRUE
and
if (true || true AND false); // is the same as:
if ((true || true) && false); // FALSE
because AND has a lower precedence and thus || a higher precedence.
These are different in the cases of true, false, false and true, true, false.
See https://ideone.com/lsqovs for en elaborate example.
I guess it's a matter of taste, although (mistakenly) mixing them up might cause some undesired behaviors:
true && false || false; // returns false
true and false || false; // returns true
Hence, using && and || is safer for they have the highest precedence. In what regards to readability, I'd say these operators are universal enough.
UPDATE: About the comments saying that both operations return false ... well, in fact the code above does not return anything, I'm sorry for the ambiguity. To clarify: the behavior in the second case depends on how the result of the operation is used. Observe how the precedence of operators comes into play here:
var_dump(true and false || false); // bool(false)
$a = true and false || false; var_dump($a); // bool(true)
The reason why $a === true is because the assignment operator has precedence over any logical operator, as already very well explained in other answers.
Here's a little counter example:
$a = true;
$b = true;
$c = $a & $b;
var_dump(true === $c);
output:
bool(false)
I'd say this kind of typo is far more likely to cause insidious problems (in much the same way as = vs ==) and is far less likely to be noticed than adn/ro typos which will flag as syntax errors. I also find and/or is much easier to read. FWIW, most PHP frameworks that express a preference (most don't) specify and/or. I've also never run into a real, non-contrived case where it would have mattered.