PHP can if statement have two values? - php

I was trying out a PHP "if" statement in which I want two things to be true: that $myvar is equal to 1 and that $myvar2 is equal to 2. However when I tried this:
if($myvar=='1', $myvar2=='2') {
header("location:index.php");
}
It failed to work. Is there a way to set up one if statement to contain these two variables like I have presented?
Thank you

You can use the boolean AND operator (&&)
if($myvar=='1' && $myvar2=='2') {
header("location:index.php");
}
Here's a full list of operators: http://php.net/manual/en/language.operators.logical.php

This will do. It is essential that you learn more about operators and flow control.
if($myvar=='1' && $myvar2=='2') {
header("location:index.php");
}

You need to use "and" operator &&

The && operator is the same as writing x='1' AND y='2',
So in ur case
if($myvar=='1' && $myvar2=='2') {
You can read more about PHP's operators at http://php.net/manual/en/language.operators.logical.php

Related

I want use && operator and Not operator at the same time in IF statement

I want use && operator and Not operator at the same time in IF statement. If it is possible then how to write it. Below is the statement which i have written is it right?
if(!$row['sl_name'] && !$row['sn_name']){
}
You can write like this if you wish to check for blank variables:
if(!empty($row['sl_name']) && !empty($row['sn_name'])){
}
else{
}
It checks for both the variables are not empty if anyone of them is empty it goes to else part.

Redirect user with IF condition

I'm like to use this IF to redirect user based on permissions. But user is all time redirected to dashboard.
Actually i have set:
$perm_edit = '0';
$user_level = '1';
if ($perm_edit !== 1 || $user_level !== 1) {
header("Location: $url/dash.php?error=1"); exit;
}
|| operator require only one condition to go ahead.
What do I wrong?
The triple comparison operators are "strict", meaning they check type as well as value. You are comparing strings to numbers, which means they will always be false. Either turn them both into numbers, or just use double comparisons: $perm_edit != 1
It is because you are using the not identical to operator (!==). It does not do type juggling, which is what you need in this case.
In order to find out if two values, of different types, are equivalent, you must use the not equal to operator (!=):
if ($perm_edit != 1 || $user_level != 1) {
header("Location: $url/dash.php?error=1");
exit;
}
Another way to write that is:
if ( ! $perm_edit || ! $user_level) {
header("Location: $url/dash.php?error=1");
exit;
}
The reason that works is because PHP juggles the types. In other words, it turns 0 into false and 1 into true, and then the ! operator (also called the NOT operator) turns it into the opposite.
You should read up on comparison operators and operators in general.

Testing if either of two condtions are true

I'm trying to test if either of two variables are true using the code below, but I the code always returns the true conditions even when the variable is blank. Have I done this correctly or is it possible the variables are always true?
Thanks in advance for your help.
<?php
if (($gogo_team_member_twitter !== true) or ($gogo_team_member_facebook !== true)) {
echo('class="amb-with-socal"');
}
else echo('class="amb-without-socal"');
?>
If you need "Testing if either of two condtions are true" then your condition should look like:
if ($gogo_team_member_twitter === true || $gogo_team_member_facebook === true)
or just
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
if you don't need strict comparison
You have the right idea, but you're checking that the variables are not true. Surely you want to check if either is true?
Also, try to use || and && rather than or and and, as they have a higher precedence.
I would just write
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
In addition when dealing with negatives like this you can use "and"
if ($gogo_team_member_twitter !== true && $gogo_team_member_facebook !== true)
Well, you are checking if any of the 2 variables is not exactly equal to true.
If the variable is blank, it is not true, and so the condition is met.

what if the index doesnt exist

I have this if statement
<?php if(in_array($product['product_id'], $selected_products['business'])) { ?>
but sometimes the array $selected_products doesnt have the index business .. how can i alter the if condition without have an outer if statement
It depends on what you want to do if the index doesn't exist.
If you only want to perform this check if the index is there, add an isset() check before it (line break for clarity):
if (isset($selected_products['business'])
&& in_array($product['product_id'], $selected_products['business'])) {
Additionally, if you need to do something else in the event the index isn't there, attach an else block.
Short circuit isset using the && operator. With short circuit evaluation, the second expression isn't evaluated if the first one fails.
<?php if(isset( $selected_products['business']) && in_array($product['product_id'], $selected_products['business'])) { ?>
Use the logical AND operator && to combine both expressions:
if (array_key_exists('business', $selected_products) && in_array($product['product_id'], $selected_products['business']))
You could use a ternary statement, that line will be a bit busy though:
<?php if(in_array($product['product_id'], isset($selected_products['business']) ? $selected_products['business'] : false))) { ?>

IF statement with AND as well as OR operators PHP

I have the following if statement
if (isset($part->disposition) and ($part->disposition=='attachment'))
Problem is the second part of that statement, i also need to include this;
($part->disposition=='inline')
The statement needs to work if the disposition is attachment or if its inline.
This must help:
if (isset($part->disposition) && ($part->disposition=='attachment' || $part->disposition=='inline'))
doesn't that work:
if (isset($part->disposition) and (($part->disposition=='attachment') or ($part->disposition=='inline')))
In case you may be going to have more than two options in the future you might also be interested in in_array(needle, haystack)
if (
isset($part->disposition)
&& in_array($part->disposition, array('attachment', 'inline', 'option3', 'option4'))
)
If you want the equivalent of === (strict comparison, instead of == like in your example) set the third parameter of in_array() to true.
This try (for efficiency):
if (isset($part->disposition))
{
if($part->disposition=='attachment' || $part->disposition=='inline')
{
// perform task
}
}

Categories