I have a for loop, but I want to skip some of the loops if a condition is met.
This works fine, and will continue the loop if $option_nm isn't "Fonts".
if($option_nm != "Fonts"){
continue;
}
However, this doesn't work, and skips everything.
if($option_nm != "Fonts" || $option_nm != "Style" || $option_nm != "Thread" || $option_nm != "Textbox"){
continue;
}
I also tried changing || to Or which does the same thing.
What am I doing wrong?
You are confusing the OR statement.
Your code should be like:
if($option_nm != "Fonts" && $option_nm != "Style" && $option_nm != "Thread" && $option_nm != "Textbox"){
continue;
}
Your if condition always return true and loop goes continue, because your if condition you use or or ||.
try this one.
if($option_nm != "Fonts" && $option_nm != "Style" && $option_nm != "Thread" && $option_nm != "Textbox"){
continue;
}
Even though your post is over a year old, I'll give another solution.
You can utilize the in_array functionality in php and do an negative if statement around your statement.
$elements = array("Fonts", "Style", "Thread", "Textbox")
if (!in_array($option_nm, $elements)) { continue;}
This makes future maintenance simple and is easy to read.
Related
I have a search on a site which I only want to display any results if something has been entered into one of the fields. I'm not really a php person so the best I could do was by adding this before the search:
<?php code on how to get the listings ...
if ($search_location != "" || $search_keywords != "" ) {
... code on how to display the listings.} ?>
As far as I understand this will only display the results if either the location or keywords are not empty. Works well so far.
My problem: there is also a dropdown to select a category ... I tried adding this like:
if ($search_location != "" || $search_keywords != "" || $search_categories != "" ) {
But the search doesn't work if you only choose a category you have to enter in one of the other fields also, so I am assuming this was wrong.
I think you need AND (which is && in php):
if (($search_location != "" || $search_keywords != "") && $search_categories != "" ) {
// ^ here
Which means:
provide (search_location or search_keywords) and search_categories
Just posting the answer I got incase any one else needs it.
I had to use !empty like -
if ($search_location != "" || $search_keywords != "" || !empty($search_categories) ) {
I don't know why !empty works and != "" doesn't, still learning.
Thanks.
Liz.
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 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.
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
}