I have taxonomies that i need called here,
if ( $the_tax->name == 'day' , 'drink_type') {
$tax_output = '';
}
return $tax_output;
}
The above code is written wrong, The taxonomy of day works, i can not figure out how to ref a second taxonomy of drink_type.
do i use a comma? or || or what () if anyone knows php syntax and can help THANKS!
You either need logical OR (which can be written as || (common syntax) or just OR (which is PHP specific so I rather do not recommend using) - see logical operators doc for more details. So your code should look like this:
if( ($the_tax->name == 'day') || ($the_tax->name == 'drink_type') ) {
// at least one condition met
}
or use in_array() if you going to have more allowed names:
$names = ['day','drink_type'];
if( in_array($the_tax->name, $names) {
// allowed name...
}
Separate the conditions with || (logical OR)
if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') {
$tax_output = '';
}
return $tax_output;
}
Given what you said, the correct syntax would be :
if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') {
// Note that you can't use id ($var == 'X' || 'Y')
The correct syntax uses two pipes in between your two conditions of the if statement. Like this,
$tax_output;
if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') {
$tax_output = '';
}
return $tax_output;
I left out your ending} because as is it will throw a syntax error. If that ending bracket is supposed to be there then you should add the part of the code that contains the opening bracket as well.
Related
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
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']))){}
I have a long if condition as follows. There are two conditions that both have to not be met, for the statement to evaluate. I did have it as a one liner with a lot of && and ! but it became unreadable. I have tried splitting it into an if elsif else, which is more readable but doesn't read well, as the first if elsif blocks have no code in them.
What would be the best practice to tidy this code block?
if ($instructionObject->instruction=='nesting_grammar' && $instructionObject->match=='>'){ //if instruction is a '>' child indicator
//don't change the child depth
}else if ($instructionObject->instruction=='selector' && is_object($this->instructions[$key+1]) && $this->instructions[$key+1]->instruction == 'nesting_grammar' && $this->instructions[$key+1]->match == '>'){ //if instruction is a selector followed by a '>'
//don't change the child depth
}else{
$insertOffset += $childDepth;
unset($childDepth);
}
You can use "extract method" refactoring. Replace your conditions to new methods.
if ($this->isInstructionNestingGrammar($instructionObject)){
//don't change the child depth
}else if ($this->isIntructionSelect($instructionObject)){
//don't change the child depth
}else{
$insertOffset += $childDepth;
unset($childDepth);
}
In new methods put every compare to separate line.
P.S. Don't be afraid of long name of methods.
Just negate the conditions and skip the if and else if parts as the two initial conditions don't do anything...
if (
!($instructionObject->instruction=='nesting_grammar' &&
$instructionObject->match=='>')
|| !($instructionObject->instruction=='selector'
&& is_object($this->instructions[$key+1])
&& $this->instructions[$key+1]->instruction == 'nesting_grammar'
&& $this->instructions[$key+1]->match == '>')
) {
$insertOffset += $childDepth;
unset($childDepth);
}
Not directly answering your question, but what about something like:
if (my_check($instructionObject) || $instructionObject->instruction=='selector' && my_check($this->instructions[$key+1])) {
} else {
$insertOffset += $childDepth;
unset($childDepth);
}
function my_check($obj) {
return is_object($obj) && $obj->instruction == 'nesting_grammar' && $obj->match == '>';
}
-- you are basically doing the same thing twice, time to think about a function for that.
Personally if i'm going to span the check across multiple lines i lay it out similar to how i'd lay out a JavaScript object;
if (
great big long check line goes in here &&
another really long ugly check line goes in here too
) {
// Do this code
}
else if (
check 3 &&
check 4
) {
//Do this code
}
Pull out sub-expressions into variables. Pseudo-example:
flibjit = FlibjitManager.FlibjitInstance(this);
isFrob =
(flibjit.Froblocity >= FlibjitManager.FrobThreshold) &&
(flibjit.Type == FlibjitTypes.Frobby);
if (isFrob) {
// ...
I know this is basic boolean logic, but I'm stuck:
I am looping through database results, and for each one I need to check for the following condition:
if($old_value != $new_value)
If the above is true, the action is:
$old_value = $new_value;
But there is a secondary condition. If the row is of type "date", I need to also check that $new_value is not empty, but the action is still the same. Right now, I am doing it like this:
if($old_value != $new_value) {
if($type != date) {
$old_value = $new_value;
} elseif(!empty($new_value)) {
$old_value = $new_value;
}
I've oversimplified the above, but really that one-line action is actually several lines that I know I don't need to repeat based on the secondary condition.
But I'm at a loss on what the right way to combine the inner condition with the outer condition. If I do something like:
if(($old_value != $new_value) && ($type == 'date' && !empty($new_value))
Then the only time it would return true is when the row is of type date.
Try:
if(($old_value != $new_value) && ($type != 'date' || !empty($new_value))
That should do it. Let me know if you need help understanding why.
if(($old_value != $new_value) && ($type != 'date' || ($type == 'date' && !empty($new_value)))
Maybe this one can be made shorter but I can't think of how.
My site requires a login that establishes permissions based on the user. In the photo listing of the site, I must determine if a particular photo will be displayed if the user has guest access only. So I thought that this if else statement would work:
if (!($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')) {
// show the photo if it isn't private and the user isn't a guest
But it doesn't.
However, if I separate this test into three lines then it works just fine.
$is_private_photo = $mysql_row['guest_access'] == 'NO';
$is_guest = $_SESSION['user_level'] == 'GUEST';
$both_private_and_guest = ($is_private_photo AND $is_guest);
if (!$both_private_and_guest) {
// show the photo if it isn't private and the user isn't a guest
What is wrong with the first version?
Your first if is
if (!($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST'))
Which is actually interpreted like this :
if (
(!($mysql_row['guest_access'] == 'NO'))
AND ($_SESSION['user_level'] == 'GUEST')
)
The ! is only applied to the first condition, not both, because it has a higher level of priority than AND (see Operator Precedence)
Your condition should probably be re-written with some additional parentheses :
if (!(($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')))
So the ! is applied to the whole condition.
The logical not operation needs to be applied to the AND results, but in your code sample it only applied to the first clause (NOT has higher precedence than AND). You can solve it by grouping both conditions:
if (!(($mysql_row['guest_access'] == 'NO' AND ($_SESSION['user_level'] == 'GUEST'))) {
The not operator (!) only covers the first condition so either add another bracket, or do this
if ( $mysql_row['guest_access'] != 'NO' AND...
It's because you put the parentheses in the wrong place in your first version. The equivalent form of the second version is
if (!$is_private_photo AND $is_guest)
which is clearly not what you intended.
Make it like this:
if (($mysql_row['guest_access'] != 'NO') AND ($_SESSION['user_level'] != 'GUEST')) {
Your '!' is not being applied to the 'AND' statement...it is only being applied to the ($mysql_row['guest_access'] == 'NO') statement.
You are only negating the first condition. Try this:
if (!($mysql_row['guest_access'] == 'NO' AND $_SESSION['user_level'] == 'GUEST')) {
Your negation operator does not apply as expected. You should use something like:
if ( ! (($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')) )
if ( ! ($mysql_row['guest_access'] == 'NO') OR ! ($_SESSION['user_level'] == 'GUEST') )