This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 6 years ago.
(PHP)
for example, i have functions,which execute heavy queries and return TRUE or FALSE.
is there any performance difference:
if ( cond1() && cond2() && cond3() && cond4())
or
if (cond1()){
if (cond2()){
}
}
because, in the last case, if cond1() is false, then it doesnt execute the other conditions.. is same for the first example?
No this has no performance differences.
In this example when condition 1 fails, it doesn't execute the second condition
if ( cond1() && cond2() && cond3() && cond4())
Related
This question already has answers here:
Using return in ternary operator
(7 answers)
Closed 5 years ago.
I was recently refactoring. And thought this would awesome. But is not allowed.
<?php
function(){
doSomething() && return true;
}
Because return is a statement and cannot be used as part of an expression. An expression is something that returns/results in a value. return doesn't result in any value, but operands to && must result in a value for the && expression to be evaluable.
This question already has answers here:
Order in conditional statements [duplicate]
(2 answers)
Closed 5 years ago.
What is the difference between the following two statements?
Suppose we have the following variable.
$someVariable = false;
Statements;
if($someVariable === false)
and
if(false === $someVariable)
There is no difference you can use it any way you want.You are just checking/comparing 2 values whether they are equal or not.
In simple words it's just like Is 1+2=3 or 2+1=3??
This question already has answers here:
PHP If statement always firing as true
(2 answers)
Closed 5 years ago.
I'm having issues with AND/OR in an if/else PHP statement. This is one of the codes in particular:
if(is_page_template('page-home.php') || ('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
If it's like this - with || / OR , it loads every паге template with the first class_trans option and not just the 2 templates specified, and if you put AND instead of OR, then it loads just the first one ('page-home.php') with the first class_trans and doesn't load the second one ('zaadvokati.php') with it. I suppose that, for some reason, it doesn't recognize the second template, but I don't know why.
Can you help?
Thanks!
You need to reiterate the function in the condition. try this:
if(is_page_template('page-home.php') || is_page_template('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
This question already has answers here:
How do i make shortcuts for IF statements?
(5 answers)
Closed 8 years ago.
I have a function that checks user permissions and I want it to do this:
if(verify("O") || ("M"))
so that it checks if the user has the permission "O" or "M" but it does not work
You're close. This should work:
if(verify("O") || verify("M"))
If your function verify() only accepts one input. You need to call it again with the second input. Now, if either verify("O") or verify("M") returns true, your IF statement will return true.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if mysql returns null/empty
I have SQL command (LEFT JOIN between 2 tables) with result 'NULL' or '0'. And now I want to create PHP condition like this :
if ($rowSearch['Approved'] == NULL)
but it failed... then I tried this one
if ($rowSearch['Approved'] == 'NULL')
also failed... PHP always skip that statement. how to write PHP IF statement for this? thanks before.
If you need to distinguish between NULL, "", False, and 0, then you need to use a strict comparison ===. Or you can use is_null(). The table on this page may be relevant.
if ( empty($rowSearch['Approved']) && $rowSearch['Approved'] !== 0 )
You need to compare with ===
if ($rowSearch['Approved'] === null)