How to add a second condition to this equal statement? - php

I am using this snippet below, how can i add a second equal statement with an "and" so something like ($onevariable === $othervariable) and ($2variable ===$2othervariable)
if ( $onevariable === $othervariable ) {
require("file.php");
}

You do this with &&
if ( $onevariable === $othervariable && $2variable === $2othervariable) {
You can read more about logical operators here.

You can Directly use the && operator like this
if ( $onevariable === $othervariable && $2variable ===$2othervariable )
{
require("file.php");
}
further You can visit here

This is fairly straight-forward, it's just how you already wrote in your question:
if ( ($onevariable === $othervariable) and ($2variable === $2othervariable) ) {
require("file.php");
}
See as well Logical Operators (PHP).
Note: $2variable is not a valid variable name, as well isn't $2othervariable because they start with a number. But I think you get the idea from the example code.

You can use a simple && operator:
if ( $onevariable === $othervariable && $2variable === $2othervariable ) {
require("file.php");
}

Just use the logical and operator &&. You can also use and if you prefer, but && is more in use. The only difference between the two is operator precedence.
if ($onevariable === $othervariable && $2variable === $2othervariable) {
require("file.php");
}

Related

I can't figure out php syntax

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.

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

Multiple conditions in the ternary operator safe?

I have seen advice that says the ternary operator must not be nested.
I have tested the code below and it works okay. My question is, I haven't seen the ternary operator used like this before. So, is this as reliable as it were used in an if or could something like this come and bite me later(not in terms or readability, but by failing).
$rule1 = true;
$rule2 = false;
$rule3 = true;
$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true)) ? true : false;
if($res) {
echo "good";
} else {
echo "fail";
}
Thanks!
If the results you are returning from the ternary operator are only "true" and "false", then you don't even need the operator. You can just have:
$res = (($rule1 === true) && ($rule2 === false) && ($rule3 === true))
But, to answer your question, yes multiple conditions work perfectly well.
It's totally legal, it works and is "as reliable as if", but it looks ugly.
If you put each ternary statement inside parenthesis, nesting would be fine too:
$res = ( $rule1 ? true : ( $rule2 ? true : false ) )
The only thing that is advised against in the manual is nesting without parenthesis like this:
$res = ( $rule1 ? true : $rule2 ? true : false )
Is there a reason you want to have your conditions saved into a variable? this is the simplified version of above.
if($rule1 && !$rule2 && $rule3)
{
echo "good";
}
else
{
echo "bad";
}
You do not need the ternary if you are going to return true or false. Quoting the manual:
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
This means
$res = (($rule1 == true) && ($rule2 == false) && ($rule3 == true));
will assign true or false already. Also, if dont care about $rule being booleans, you dont need the comparison with ==. You also dont need the braces, e.g.
$res = $rule1 && !$rule2 && $rule3;
is the same as your initial ternary.
A good practise when you have multiple expressions like that is to hide the actual comparison behind a meaningful method or function name, e.g.
function conditionsMet($rule1, $rule2, $rule3) {
return $rule1 && !$rule2 && $rule3;
}
and then you can do
if (conditionsMet($rule1, $rule2, $rule3)) {
// do something
}
Of course, conditionsMet isnt that meaningful. A better example would be something like isSummerTime or isEligibleForDiscount and so on. Just express what the rules express in the method name.
You might also be interested in Simplifying Conditional Expressions from the book Refactoring - Improving the design of existing code.
You can also do
$res = ($rule1 && !$rule2 && $rule3);
It's legal and doesn't have to be "ugly". I use the "hook" operator often, in table form it's quite clean, e.g.:
bool haveANeed()
{
// Condition result
// ---------- ------
return needToEat() ? true
: needToSleep() ? true
: needToStudy() ? true
: needToShop() ? true
: needToThink() ? true
: false; // no needs!
}
This function would, IMHO, be less clear and certainly longer if written with if-else logic.

PHP - not operator, any other aliases?

if(!($whatever && what()) do_stuff...
Can this be replaced with something more intuitive like:
if(not($whatever && what()) do_stuff...
?
function not($OriginalCheck)
{
return !$OriginalCheck;
}
function is($OriginalCheck)
{
return !!$OriginalCheck;
}
should do exactly that :)
There are several ways to write checks:
if(!($whatever && what()) do_stuff...
if(!$whatever || !what()) do_stuff...
if(($whatever && what()) === false) do_stuff...
if((!$whatever || !what()) === true) do_stuff...
if($whatever === false || what() === false) === true) do_stuff...
all these ways are intuitive and known through out the programming world.
No it can't. See http://www.php.net/manual/en/language.operators.logical.php for the language reference about logical operators, and navigate to find other aliases.
Note however that the precedence of && and || is not the same as and and or.
One option is to make the boolean expression more explicit:
if(($whatever && what()) == false) // do_stuff...
Or alternatively, by implementing a custom not():
function not($expr) {
return $expr == false;
}
if(not($whatever && what())) // do_stuff...
Actually, I don't like the ! operator as well. Although it was understood by every developers but it took me a bit of time every time I read the code. So I prefer do not use the ! operator. But it depends on also.
For php built-in function, I created almost the helper functions for not operator.
https://github.com/vuvanly/not-operators-helpers
There is no alternative !, but it could be written less intuitive:
if ($whatever - 1) {
}
Should the question cause be that ! is too easy to overlook, but not more visible; then another alternative notation would be:
if (!!! $whatever) {
If this still looks to simple, just use:
if (~$whatever & 1) {
Binary operations always look professional ;)

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