PHP: Correct use of && in an if statment - php

I am trying to ensure that both conditions are true in my statement. Is the following the correct way to do so?
I'm not sure of my bracket placement etc.
if((functionOne($paramOne) && functionTwo($paramTwo)) == condition){
....

The correct way is
if(functionOne($paramOne) == condition && functionTwo($paramTwo) == condition){

Related

Multiple Conditions not working in "if"

I am using multiple conditions in if, please see the code below;
if (strpos(serialize($row['pirority']),"P1")!==false &&
strpos(serialize($row['product']),"WFS")!==false)
{ $wfsp1++; }
If i add another condition it doesn't work;
if (strpos(serialize($row['pirority']),"P1")!==false &&
strpos(serialize($row['product']),"WFS")!==false) ||
strpos(serialize($row['product']),"DayEnd")!==false)
{ $wfsp1++; }
Can any one please guide me, what could be the reason?
You have syntax error.Try this
if (strpos(serialize($row['pirority']),"P1")!==false &&
strpos(serialize($row['product']),"WFS")!==false || //extra parenthesis was given
strpos(serialize($row['product']),"DayEnd")!==false)
{ $wfsp1++; }

Display HTML if two conditions are true or another two are true or third part of conditions are true

I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.
I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .
To explain in the easiest possible way I want to do this:
if(!empty($Options_arr[0]) && $Options_arr[0]!="") or
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or
(!empty($Special_arr[0]) && $Special_arr[0]!="")
{?> some HTML here
All help will be appreciated thank you.
empty() already checks for empty string "" so it's shorter:
if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
//some HTML here
}
BragG, you can use elseif
Like:
if((!empty($Options_arr[0]) && $Options_arr[0]!="") ||
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") ||
(!empty($Special_arr[0]) && $Special_arr[0]!=""))
{
// some html or any code
}
I hope that is what you were looking for..
Feel free to ask any question.
You are just missing some brackets. Also || is more frequently used than OR
if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) && $Special_arr[0]!="")){
echo '<p>hello</p>';
}
You're basically already there...
if (
(!empty($Options_arr[0]) && $Options_arr[0]!="")
|| (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
|| (!empty($Special_arr[0]) && $Special_arr[0]!="")
){
...do something
Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs

PHP if status and !isset or !isset

I'm trying to match a condition where if the user status is 10 and ANY POST variables are not set it triggers an error:
if ($_SESSION['status']=='10' && !isset($_POST['a']) || !isset($_POST['B'])) {}
I can not use && conditions for any !isset as one variable may be set though another might not. I only want the condition to match if one or more variables are not set AND the status==10.
When testing if a $_POST variable !isset, I remove an input element from the page via a browser web tool (e.g. Firebug). When the form is submitted with the variable missing it's still passing validation incorrectly.
I am also seeking a PHP if grouping condition.
If you are looking for absolutely any PHP variables, I'd recommend this:
if (($_SESSION['status'] == 10) && (count($_POST) > 0)) {
You can then get the list of _POST var keys using array_keys($_POST).
If you are looking for a specific:
if (($_SESSION['status'] == 10) && (isset($_POST['A']) || isset($_POST['b']))) {
The order of the brackets is important. You can separate groups of logical statements with brackets.
Is that was what you were looking for?
$status = $_SESSION['status'];
if($status == '10'){
if(!isset($_POST['a']) or !isset($_POST['B'])){
//Triggers error.
}else{
//Another
}
}
Try making it a function:
function checkAllVars($dataVars, $requestVars) {
foreach($dataVars as $varname) {
if(!isset($requestVars[$varname])) {
return false;
}
}
return true;
}
$dataVars = array (
"varName1",
"varName2",
"varName3",
"varName4",
);
$allVarsSet = checkAllVars($dataVars, $_REQUEST);
you might be looking for
if($_SESSION['status']=='10' && (!isset($_POST['a']) || !isset($_POST['B']))){}
^ ^
which means if status = 10 and (if not set 'a' or not set 'B' or they can be both not set) do something
or you might be looking for
if(($_SESSION['status']=='10' && !isset($_POST['a'])) || ($_SESSION['status']=='10' && !isset($_POST['B']))){}

php if and or syntax

I'm struggling to make this statement work
if(((!isset($_COOKIE['email']) && !$_SESSION['remember_me']) || $_SESSION['id']) {
//do something
} else {
//do something
}
I want it to return true if both of the first two statements are met, or if the third statement is met?
You have one too many ( at the start of the line. Other than that, there's no reason why it shouldn't work.
if ( ((!isset($_COOKIE['email'] && !$_SESSION['remember_me'])) || $_SESSION['id'] )
That should work, if i correctly understand what you want

Using OR in a IF

Any ideas why this isn't working?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books') {
//do something
}
I think you mean AND instead of OR because you're using not equals.
By using not equals in the way you are the statement will always be true, if $page->Slug equals 'water-filters' it doesn't equal 'pet-care' and hence the if statement as a whole returns true.
if($page->Slug != 'water-filters' && $page->Slug != 'pet-care' && $page->Slug != 'books')
{
//do something
}
I'm guessing that "do something" is always getting executed?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books')
{
//do something
}
For any value of $page->Slug, it will always be not equal to ONE of those three conditions, therefore at least one (technically, at least two) of the statements will always be true. Since you're using an 'OR' as long as one of the three statements is true, the whole thing will be true.
Therefore, this is essentially saying
if (true) {
//do something
}
$page->Slug is either 'water-filters' or 'pet-care' or 'books'
Try
== or == or ==
or
!= and != and !=
:-D
If the Slug is not "water-filters" or is not "pet-care" or is not "books"...
Well, if it's one of those, or any other value, it's by definition not the other two (or not all three). So this condition is always true.
Aside from what the others have said above, which are correct also, try this syntax for readability.
if(!in_array($page->Slug, array('water-filters', 'pet-care', 'books')) {
// Do something
}

Categories