Proper use of parentheses in if with and/or statements PHP - php

This is kind of a ridiculous question...mostly to hopefully prove my old man programmer friend wrong.
I write if statements like this (ones with AND/OR in them), as I have always known...
if($something == $something OR $whatever == $whatever){stuff...}
My old man friend does this...
if(($something == $something) OR ($whatever == $whatever)){stuff...}
I have never seen anyone do it like that before. It does run without error. I told him that is invalid code but he shrugs it off and says it runs. Is it valid? I mean...it does run. Extra characters so mine still wins. Was this an old way of doing it in PHP? I can't find any info on the latter style. Just kind of curios. Thanks.

Both are valid ways, the second one, with the extra parentheses is more readable.
Why use an extra set of parenthesis when using PHP boolean test in an IF()?

Parenthesis are also called grouping operators which are used so that expressions with lower precedence can be evaluated before an expression with higher priority.
Inside if statement expression is needed which is parsed depending upon the precedence of the operators using some algorithm like Shunting Yard.
So it does not matter how many grouping operator you add to some expression which is already guaranteed to be evaluated before other binary operators( OR in your case)
For eg. 4*3 + 5 is same as (4*3) + 5( as * has higher priority and will be evaluated before)
Acc. to shunting yard your postfix expression is
$something $something == $whatever $whatever == OR
and your old friends postfix is also
$something $something == $whatever $whatever == OR
Only difference is yours is micro faster while his one's is probably more readable

The old man's method is not incorrect. It can be written both ways. That is due to the fact that you can evaluate a complex boolean expression all the way down to true or false which is all that is required. If someone said write this expression with as few parenthesis as possible, then your method would be "right".

That is completely valid, and some may argue that parenthesis are better for ease of readability, however there is no right way.
What if you had three... :O
syntactically, something in this format
if ( a == b OR a == c OR a == d )
could be completely different than something in this format
if ( a == b OR ( a == c OR a == d ) )
if ( ( a == b OR a == c ) OR a == d )

Related

Why use an extra set of parenthesis when using PHP boolean test in an IF()?

I'm sorry the title of this question is odd. I couldn't find a good way to word it!
The idea is simple, sometimes you see PHP tests this way:
if (!a_function("something")) { }
Here you can think of it as "if not true". I sometimes see the exact same thing but with extra parenz:
if (!(a_function("something"))) { }
Why does it require the extra parenz after the bang? Don't they both essentially mean if (!true)?
For extra bonus, what are the reasons for the two styles (does this have a name?) and maybe give examples of how they would give alternate results if not used correctly.
update:
Here is an example in a PHP script I'm using, the author is testing environment variables and seems to use the styles interchangeably:
if (!(extension_loaded("iconv"))) { ... }
if (!(extension_loaded("xml"))) { ... }
if (!function_exists("json_encode")) { ... }
if (!ini_get("short_open_tag")) { ... }
I know you can't answer for the programmer here, but why would they be alternating the use of extra parenz when these small functions are right next to each other?
I happen to know that, for example, the return value of ini_get is just the number 1, and the return value of the extension_loaded functions may also just be the number 1, so it seems like there would be no difference. I'm not 100% sure there isn't some other trick to this than simple preference or order of operation.
update 2:
I understand parenz can be used for either clarity, or order of operations, but I'm not convinced it is only personal preference beyond that.
In my example above, everything depends on what is returned by the functions that are being tested.
It's my understanding that by wrapping a statement in parenz, PHP will force it into a bool. But when it's not in parenz, could there be a return value that breaks the code without using the parenz around it to force a bool?
If people say, in my example code above, that there is nothing but personal preference going on, then I'll just have to accept that, but I have my doubts.
the parenthesizes are used in case if there are more than 1 logical operator with different precedence, to indicate that "!" operator must be applied after all other operators have been processed. For example:
if(!($var1 < $var2))
First will be checked if $var1 is less than $var2, and after that will be checked if the result is false.
If use that:
if(!$var1 < $var2)
then firstly will be checked if $var1 is false and the result will be compared to $var2, that simply do not make sense.
It's not required. It's a matter of personal preference. Sometimes you like to have extra parens to be EXTRA certain of how the expression will be evaluated.
if(a or b and c)
is confusing.
if ((a or b) and c)
is much more clear.
if(a or (b and c))
is much more clear.
They both work, but some people might have different opinions on which one is more readable.
Parenthesis are not required in the given case, but they can be if, for example, you also assign a variable at the same time :
if (($myVar = myFunc()) !== false) {
// Doing something with $myVar, ONLY if $var is not false
}
While, in the following case, it will change the logic
if ($myVar = myFunc() !== false) {
// Here $myVar = true or false instead of the wanted value
}
if( !(should_return_trueA() && should_return_trueB())) {
// at least one have returned false
}
esentially is the same as:
if( !should_return_trueA() || !should_return_trueB() ) {
// at least one have returned false
}
It's, in my case, a practice to avoid mistaken/ommited exclamation marks. Useful, when building more complex conditions and looking for all-false or all-true result.

Using and or in conditionals for readability instead of && ||

Often when I am writing PHP I construct conditionals like this:
if(1 === $var1
and 2 === $var2
or 1 == $var3) {
// do something here maybe
}
I think it makes them easier and more natural to read. I know this is probably laughable when I am using Yodas though.
The reason I have each condition on its own line prefaced by and or or is because it makes it easier to comment part of the statement out when debugging.
Is there any disadvantage to doing this compared with the "usual" || and &&?
or and and do not have the same operator precedence as || and &&. This means that in for certain values of expressions[a] and [b], [a] and [b] != [a] && [b]. This may create non-obvious bugs.
Note that one is higher precedence than assignment, while the other is not. This is a subtle difference, and even if you understand it, other developers reading your code may not. As a result, I personally recommend using only && and ||
Ruby's operators are similar to PHP's in this regard. Jay Fields published a simple example of the difference.
There's no significant functional difference in PHP between and and &&, nor betweeen or and ||.
The only difference between them is that they are considered as different operators, and have different positions in the order of operator precedence. This can have a big impact on the results of complex operations if you neglect to wrap things in brackets.
For this reason, it is generally a good idea to use one or other style of operators, and stick with it, rather than mixing between the two in the same code base (this is also a good idea for readability). But the choice if which pair of operators to use is fairly immaterial.
Other than that, they are basically the same thing.
See here for the PHP operator precedence: http://php.net/manual/en/language.operators.precedence.php
(note the different positions in the table of the and and or vs && and ||)

Logical Operators: is AND better than OR?

Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing something obvious.
So let's say some user input throws an error, and I want to catch it and return some feedback. The error will be a number, 0 - 8. 0 means "No Error". I want to give the user very specific feedback if the error is 3 (No numbers allowed, let's say). For the other 7 possibilities, I just want to end the script and echo the error.
I was working this out and decided to go with this:
$no_errors ($_error != 0 || $_error != 3) ? FALSE : TRUE;
if (!$no_errors)
echo $error_msg['$_error'];
$error_3 ($no_errors && $_error == 3) ? TRUE : FALSE;
if ($error_3)
bunch of stuff happens;
else
bunch of other stuff;
Anyways, I was then noticing the OR operator on the first line and was thinking that it might be better/safer to user an AND operator. But the more I contemplate, the less I see a difference.
So the real question is, if you want to eliminate two possibilities of a specific variable, are AND and OR identical, or is one logically/functionally more optimal?
It will be much more readable if you use a switch statement:
switch ($_error) {
case 0;
nothing happens;
break;
case 3:
bunch of stuff happens;
break;
default:
bunch of other stuff;
break;
}
First of all, I think I would recommend using another way of identifying errors than using "magic numbers". They are hard to maintain, as you easily forget what "3" meant. It looks like your language is PHP, which has support for exceptions. I'd recommend using them instead, you can read more about them here: http://dk.php.net/exceptions
As for logical operators, there aren't really any that are considered "good practice". Use what you want. If you have trouble figuring out when your expression is true/false, try making a truth table: http://en.wikipedia.org/wiki/Truth_table
Performance-wise, keep in mind that that the evaluation will be lazy in most languages. Using OR, if the first condition is true, it will return true without evaluating the second condition. For AND, it will return false if the first conditions is false, without evaluating the second.
Otherwise, the performance of the operators themselves is not really different. Use what is most readable to you.
logically the following are identical ( excuse my pseudo code )
(! expression_one || ! expression_two) /** this is the same as the one below **/
! (expression_one && expression_two)
Functionally which one is more optimal? They are both as optimal as each other. Both ways (&& and ||) allow short circuiting if the first expression is true (in the || case) or false ( in the && case)
The best is the one that helps you read code faster. It is the only true optimization you can make here, and possibly in millions of other places.
This would look more simple to read:
if($_error==3)
do stuff;
else if($_error>0)
do some other stuff
else
do normal stuff
Nobody notices the microseconds that you may win.
Personally I would eliminate the use of those numbers and use constants instead. Besides being easier to maintain, they make the coding itself much easier and allows you to update the values tomorrow e.g. if some circumstance foreces you to change the number from 3, you have to pretty much look through all your code
As suggested by gkrogers, a switch
while accomplishing the same thing
is much easier to read and maintain
Performance wise I believe they are very similar (see post by wvanbergen), however there are obvious differences in behaviour. I think that your current posting may not be doing what you are hoping for. In the first line if you $_error = 3 then you will have $no_errors = false. As you are checking for two conditions that both need to be satisfied maybe an and would be more appropriate.
Sometimes I find the easiest way to check my logic is to actually say what I want out loud. For example, "We have no errors if the error code is not 0 and not 3"
I generally find that in situation like this with only a few variables, the way I would write the sentence in English provides me with the most intuitive (and accurate) method for determining the logical operators.
If you find that the code looks messy after this process then you may find this link on De Morgan's laws useful
(A || B) = !(A && B). So it really doesn't make a difference
EDIT: (A || B) = !(A && B) is wrong. Tks for the comment. The correct form is (!A || !B) = !(A && B).
My apologies
If you use:
$no_errors ($_error != 0 && $_error != 3) ? FALSE : TRUE;
This means if $error != 0 AND $error != 3 which is invalid logic because $error cannot be == to 3 and 0 it can be 3 OR 0 but not both.
In the above example if $error = 0 then it would evaluate to FALSE because it is not 3 && 0 you are looking for it to be either 3 || 0.

Why isset() is more efficient to test than '==='?

Summarized, I made a loop with a few iterations to check the efficiency of each test:
$iterations = 99999999;
$var = null;
isset comparasion
if ( isset( $var ) )
{
}
'===' comparasion
if ( $var === null )
{
}
And i have this log, in microseconds:
'isset()': 1.4792940616608
'===': 1.9428749084473
For me, this is a little curious. Why isset() function is faster than one comparison operator as ===?
The === comparison is a strict check, meaning that the two objects you're comparing have to be of the same type. When you break it down in plain English, it's actually not that weird that === needs some more time. Consider the parser to do this:
if (isset($var)) {
// Do I have something called $var stored in memory?
// Why yes, I do.. good, return true!
}
if ($var === null) {
// Do I have something called $var stored in memory?
// Why yes, I do.. good! But is it a NULL type?
// Yes, it is! Good, return true!
}
As you can see, the === operator needs to do an additional check before it can determine if the variable matches the condition, so it's not that strange that it is a little bit slower.
Isset is not a function: it is a language built-in. Using isset is faster than using a function.
The other thing is that isset is used all over the place, so it makes sense that it's been profiled to death, whereas === maybe hasn't received as much love.
Other than that, you'd have to dig in the PHP source with a profiler to see exactly what's going on.
I'm not sure I would call 100 million "a few iterations". You appear to have accumulated about a half-second difference, divide that by 100 million and you get a whopping 5 nanosecond difference per iteration if my math is correct. With the difference being so small it may simply come down to the fact that isset only has one operand in this context and === has two.
It's impossible to even discuss the Zend engine's implementation details of the two examples without specifying a specific PHP version; source code is a moving target. Even minute changes to the implementations are going to effect the results over that many passes. I would not be surprised if you found the opposite to be the case on some versions of PHP and/or in a different context.
isset itself is covered by three different op-codes in the VM depending upon the context:
"Simple" Compiled Variables like your example: ZEND_ISSET_ISEMPTY_VAR
Arrays: ZEND_ISSET_ISEMPTY_DIM_OBJ (requires 2 operands, the var and the index)
Object properties: ZEND_ISSET_ISEMPTY_PROP_OBJ (also 2 operands, var and prop name)
It's an interesting question for curiosity's sake but we're in hair splitting territory and it's probably not a real-world optimization strategy.

if statement condition optimisation

I have an if statement with two conditions (separated by an OR operator), one of the conditions covers +70% of situations and takes far less time to process/execute than the second condition, so in the interests of speed I only want the second condition to be processed if the first condition evaluates to false.
if I order the conditions so that the first condition (the quicker one) appears in the if statement first - on the occasions where this condition is met and evaluates true is the second condition even processed?
if ( (condition1) | (condition2) ){
// do this
}
or would I need to nest two if statements to only check the second condition if the first evaluates to false?
if (condition1){
// do this
}else if (condition2){
// do this
}
I am working in PHP, however, I assume that this may be language-agnostic.
For C, C++, C#, Java and other .NET languages boolean expressions are optimised so that as soon as enough is known nothing else is evaluated.
An old trick for doing obfuscated code was to use this to create if statements, such as:
a || b();
if "a" is true, "b()" would never be evaluated, so we can rewrite it into:
if(!a)
b();
and similarly:
a && b();
would become
if(a)
b();
Please note that this is only valid for the || and && operator. The two operators | and & is bitwise or, and and, respectively, and are therefore not "optimised".
EDIT:
As mentioned by others, trying to optimise code using short circuit logic is very rarely well spent time.
First go for clarity, both because it is easier to read and understand. Also, if you try to be too clever a simple reordering of the terms could lead to wildly different behaviour without any apparent reason.
Second, go for optimisation, but only after timing and profiling. Way too many developer do premature optimisation without profiling. Most of the time it's completely useless.
Pretty much every language does a short circuit evaluation. Meaning the second condition is only evaluated if it's aboslutely necessary to. For this to work, most languages use the double pipe, ||, not the single one, |.
See http://en.wikipedia.org/wiki/Short-circuit_evaluation
In C, C++ and Java, the statement:
if (condition1 | condition2) {
...
}
will evaluate both conditions every time and only be true if the entire expression is true.
The statement:
if (condition1 || condition2) {
...
}
will evaluate condition2 only if condition1 is false. The difference is significant if condition2 is a function or another expression with a side-effect.
There is, however, no difference between the || case and the if/else case.
I've seen a lot of these types of questions lately--optimization to the nth degree.
I think it makes sense in certain circumstances:
Computing condition 2 is not a constant time operation
You are asking strictly for educational purposes--you want to know how the language works, not to save 3us.
In other cases, worrying about the "fastest" way to iterate or check a conditional is silly. Instead of writing tests which require millions of trials to see any recordable (but insignificant) difference, focus on clarity.
When someone else (could be you!) picks up this code in a month or a year, what's going to be most important is clarity.
In this case, your first example is shorter, clearer and doesn't require you to repeat yourself.
According to this article PHP does short circuit evaluation, which means that if the first condition is met the second is not even evaluated.
It's quite easy to test also (from the article):
<?php
/* ch06ex07 – shows no output because of short circuit evaluation */
if (true || $intVal = 5) // short circuits after true
{
echo $intVal; // will be empty because the assignment never took place
}
?>
The short-circuiting is not for optimization. It's main purpose is to avoid calling code that will not work, yet result in a readable test. Example:
if (i < array.size() && array[i]==foo) ...
Note that array[i] may very well get an access violation if i is out of range and crash the program. Thus this program is certainly depending on short-circuiting the evaluation!
I believe this is the reason for writing expressions this way far more often than optimization concerns.
While using short-circuiting for the purposes of optimization is often overkill, there are certainly other compelling reasons to use it. One such example (in C++) is the following:
if( pObj != NULL && *pObj == "username" ) {
// Do something...
}
Here, short-circuiting is being relied upon to ensure that pObj has been allocated prior to dereferencing it. This is far more concise than having nested if statements.
Since this is tagged language agnostic I'll chime in. For Perl at least, the first option is sufficient, I'm not familiar with PHP. It evaluates left to right and drops out as soon as the condition is met.
In most languages with decent optimization the former will work just fine.
The | is a bitwise operator in PHP. It does not mean $a OR $b, exactly. You'll want to use the double-pipe. And yes, as mentioned, PHP does short-circuit evaluation. In similar fashion, if the first condition of an && clause evaluates to false, PHP does not evaluate the rest of the clause, either.
VB.net has two wonderful expression called "OrElse" and "AndAlso"
OrElse will short circuit itself the first time it reaches a True evaluation and execute the code you desire.
If FirstName = "Luke" OrElse FirstName = "Darth" Then
Console.Writeline "Greetings Exalted One!"
End If
AndAlso will short circuit itself the first time it a False evaluation and not evaluate the code within the block.
If FirstName = "Luke" AndAlso LastName = "Skywalker" Then
Console.Writeline "You are the one and only."
End If
I find both of these helpful.

Categories