Is this PHP comparison the same? - php

I basically have this PHP code:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == 1 || $num_1 == 2 ){
// Do something
}
?>
What I know is that $num_1 can either be 1 or 2. This is the value I expect from $_POST['num_1']
What I want to know is that instead of using if( $num_1 == 1 || $num_1 == 2 ) could I use or is that the same to use:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == ( 1 || 2 ) ){
// Do something
}
?>
The difference here is that previously I was using if( $num_1 == 1 || $num_1 == 2 ){} but now I am using if( $num_1 == ( 1 || 2 ) ){}
I am trying to wrap my head around this. Could anyone provide a helpful explanation? Which is more efficient and best practice?

Not the same. You can test it just by doing something like that:
$num_1 = 1;
var_dump($num_1 == ( 1 || 2 )); //true
var_dump($num_1 == 1 || $num_1 == 2); //true
$num_1 = 2;
var_dump($num_1 == ( 1 || 3 )); //true
var_dump($num_1 == 1 || $num_1 == 3); //false
The main reason for this is that when you are trying to do this:
if(1 || 2)
PHP will convert integers to booleans. So, your expresion become:
if(true||true) //what is actually true

Related

Using 'and' and 'or' in an if/else PHP statement

I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..

Php OR operator gives wrong result?

I am surprised with the output of OR operator in php
$a = 5;
echo $b = ((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
It echo 5 but i expect NULL
It should be like this.
$a = 5;
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
As you stated, your $a is not 8 or 2. So assume it's 5.
How your evaluation works:
((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
Compare intval($a) == 8.
No matter what you get from #1, do || 2 which leads to true (non-zero number is loosely equals true)
Compare intval($a) != 0 - leads to true.
Compare true && true => true
Answer is $a.
Step-by-step:
(intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL;
(false || 2) && intval($a) != 0 ? $a : NULL);
(false || true) && intval($a) != 0 ? $a : NULL;
true && intval($a) != 0 ? $a : NULL;
true && true ? $a : NULL;
$a;
TL;DR
To get NULL, change || 2 to || intval($a) == 2
change this
intval($a) == 8 || 2
to
intval($a) == 8 || intval($a) == 2
i.e
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
The way you write comparison for OR is not possible in PHP. Use below code.
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);

php search within string for word?

I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}

How to do decompose conditional refactoring?

I've this situation in code where i think the code is unnecessary complex and i believe i can refactor it to make it more easier to understand and read.
So i googled about it and found decompose conditional refactoring, but i'm still in doubt how to do refactoring
if(count($bagTypes) == 1 && (array_key_exists('type1', $bagTypes)
|| array_key_exists('type2', $bagTypes)
|| array_key_exists('type3', $bagTypes))){
$flag = 1;
}
if(count($bagTypes) == 2 && (
(array_key_exists('type1', $bagTypes) && array_key_exists('type2', $bagTypes)) ||
(array_key_exists('type1', $bagTypes) && array_key_exists('type3', $bagTypes)) ||
(array_key_exists('type2', $bagTypes) && array_key_exists('type3', $bagTypes)))
){
$flag = 1;
}
Is there any better way of doing this?
You could try something like this:
$arrayKeys = array(
'type1',
'type2',
'type3'
);
$bagTypesKeys = array_keys($bagTypes);
if ((count($bagTypes) == 1 && count(array_diff($arrayKeys, $bagTypesKeys)) < 3)
|| (count($bagTypes) == 2 && count(array_diff($arrayKeys, $bagTypesKeys)) < 2))
{
$flag = 1;
}

PHP basic comparison with negative

I cannot find an answer to this and I am sure it is right in front of me. How do I say in PHP as part of an IF statement the following:
if NOT ( (variable1 == 10) && (variable2 == 20) )
Thanks!
You can use ! to achieve this
if ( (variable1 != 10) || (variable2 != 20) )
Or simply
if (!((variable1 == 10) && (variable2 == 20)))
if NOT ( (variable1 == 10) && (variable2 == 20) )
==>
if (! ( (variable1 == 10) && (variable2 == 20) ) )
<==>
if ( (variable1 != 10) ||(variable2 != 20) )
! mean not
so U can use it like this
if (! ( ($variable1 == 10) && ($variable2 == 20) ) )
or
if ( ($variable1 != 10) ||($variable2 != 20) )
&& will be || because !&& = ||
and U can't use variable1 without $ if U don't define it like this
define('variable1','value of variable1');
but then U can't change it's value so if statement will always have the same result and for that U should use $variable1

Categories