PHP logical AND of two variables - php

I'd like to do this:
$var = ($var2 . $var3)
This means $var is only true if $var1 and $var2 are truthy (anything that will cast to a boolean true).

Do it like this:
$var = ($var2 && $var3);
or simply like SQL:
$var = ($var2 and $var3);
Read more about Logical Operators and how to use them at PHP manual. Both && and and operators are good enough for it, the only difference being the precedence.

. is the string concatenation operator. Use && for a boolean AND operation:
$var = $var2 && $var3;

$var = isset( $var2 ) && isset( $var3
);
?

Related

Strange behavior with PHP 'and' operator

I am having trouble understanding the behavior of the 'and' PHP operator.
Here is the code:
$condition1 = true;
$var2 = "var2";
$var3 = "var3";
$condition2 = $condition1 and $var2 == $var3;
if($condition2)
echo '$condition1 and $var2 == $var3';
Output: $condition1 and $var2 == $var3
Now it is obvious that since $var2 != $var3, $condition2 should be false. Hence the echo statement should not be executed but it happens the other way. Can any one tell me what's wrong with this code?
Use && instead of and.
and has lesser precedence than &&. The statement
$condition2 = $condition1 and $var2 == $var3;
is executed in two steps from left to right.
1: $condition2 = $condition1 is executed. i.e. $condition2 is true now.
2: $var2 == $var3; is executed which performs the evaluation but does not assign any value to any variable.
I think this is an operator precedence issue. Try this instead:
$condition2 = ($condition1 and $var2 == $var3);
I think the issue is that your current code gets interpreted like this:
($condition2 = $condition1) and ($var2 == $var3)
Try this
$condition2 = ($condition1 and $var2 == $var3);
Or this one
$condition2 = ($condition1 && $var2 == $var3);

In PHP, why does the statement if($var1 = myfunction() && $var2 = myfunction2()) fails?

In PHP, I have seen a lot of people using the this:
if($var1 = myfunction()){
//do something
}
That way, if true the variable already holds the value you need.
Why doesn't the same work with two variables and two functions?
if($var1 = myfunction() && $var2 = myfunction2()){
// Do something
}
When i tried using the above, I always got a "1". Even though both functions return a value. As soon as I removed the second part ($var2 = ) - it worked.
Why do I get a 1?
It's because of operator precedence
php interprets
$var1 = myfunction() && $var2 = myfunction2()
expression as
$var1 = ( myfunction() && ( $var2 = myfunction2() ) )
So:
$var2 = myfunction2() comes first
Then myfunction() && $var2 is evaluated
The result of the latter expression (which is always a boolean) is assigned to $var1
The solution - use parentheses
($var1 = myfunction()) && ($var2 = myfunction2())
The better solution: avoid such expressions

How to neatly check if 4 variables are the same?

How would someone neatly check if 4 variables are the same?
Obviously this wouldn't work:
if ($var1 === $var2 === $var3 === $var4)
But what would, without writing loads of code?
One way to go would be this:
if ($var1 === $var2 && $var2 === $var3 && $var3 === $var4)
Not a huge amount of code and it gets the job done.
if(!array_diff([$var2, $var3, $var4], [$var1])){
// All equal
}
if ($var1 === $var2 && $var3 === $var4 && $var1 === $var3)
You don't need to check if 2 and 4 are equal
Using array_unique you can check if the array of unique values from the variable list is 1 (which means they are all equal):
if (count(array_unique([$var1, $var2, $var3, $var4])) == 1)
// all equal
This comes in handy especially when comparing a long list of variables, compared to a long list of == checks in an if statement.
You can use ! as your master and use &&
if($var1===$var2 && $var1 === $var3 && $var1 === $var4)
But this wont let you know which of the 4 is not like the others.
In case you are dealing with many variables, I played with the below code and it works.
$m = 'var'; //Assuming you know the variable name format $var1, $var2, $var3,...
for( $n = 1; $n <= 3; $n++) { //testing for 4 variables
$v = "$m$n";
$n++;
$w = "$m$n";
if ( $$v !== $$w ) {
echo "false";
break;
}
$n--;
}
//Breaks and echo "false" as soon as one of the variables is not equal
//Note: increase the iteration for more variable.

Concatenate two integers in php

I know it is possible in php to concatenate two strings like
$a .= $b;
which is equal to
$a = $a . $b;
Is it possible to do something with integers? but with math operations? I have two variables:
$var1 = 8;
$var2 = 2;
I need to do $var1 - $var2, but I don't want to create third variable to hold this calculation. I would try $var1 = $var1 + $var2 will this work? Is there another .= similar way of doing it?
OK, because everybody is confused now
// _Appending_ $var2 to $var1 and assign it to $var1
$var1 .= $var2;
// _Add_ $var2 to $var1 and assign the result to $var1
$var1 += $var2;
There is nothing more to remember than, that every binary operator is combineable with the assignment. Within the engine it's always expanded to
$var1 = $var1 . $var2; // You can imagine how the others looks like
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
I think you need a string formatting.
$var1 = sprintf( "%d%d", $var1, $var2 );
which will make your $var1 as 82.
As in C and C++, PHP supports Compound_assignment_operators:
So, the line:
$var1 = $var1 + $var2;
is equivalent to:
$var1 += $var2;
For full list of those operators, take a look at Compound_assignment_operators
Yes
$var1 = 1
$var2 = -1;
$var1 += $var2; // $var1 = 0
Yes, Compound Assignment operators will work in php as C & Java.
you can try like this
$Var1+=$Var2;
$var1=$var1-$var2;
will work fine, but shorthand operators like
$var1=-$var2
will not work.
I will set -2 to $var1
Notation for shorthand operator is :
$var1-=$var2;

PHP Datatype mismatch on comparison

So I have 2 variables, var1, var2.
$var1 = "53,000,000" //- integer
$var2 = 10 //- string
In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1
I get $var2 = 0 .... Why ?
.. running on PHP 5.2.14
EDIT Accidentally typed in substr_replace instead of str_replace. Updated.
I had to add a couple semicolons, but here's the code:
$var1 = "53,000,000"; //- integer
$var2 = 10; //- string
//In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
//Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1;
var_dump($var1, $var2);
And here's my output:
int(53000000) int(53000000)
I used 5.2.6, but it shouldn't matter. Do you have any other code in between what you're showing?
Use str_replace() instead of substr_replace().
You've specified the wrong parameters for substr_replace, so $var1 is evaluated to 0. I guess you wanted to use str_replace.
No need for type casting. just do the str_replace
Here is code
$var1 = "53,000,000" ;
$var2 = 10;
$var1=str_replace(',','',$var1);
if($var1 > $var2)
$var2 = $var1;
echo $var2;

Categories