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
Related
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);
This is my case.(i changed my case)
I have variable let say $var1 from database and method test($var1)
$var1 = getFromDatabase();
//now $var1 contaion "This is new variable".$var2
Then in test method i will do simple string operation from the $var1 equation.
test($var1){
$var2 = "variable 2";
//I want to extract $var1 so that string will be combined
$value = $var1;
$value_expected = "This is new variable".$var2;
}
But $value will contain "This is new variable".$var2.
I expect $value contain This is new variable variable 2 from the $var1 equation.
Is there any solution? Is that possible to do?
Thank You.
Don't use quotes. $var1 = $var2 + $var3;
Quotes mean you're working with strings
Apart from that, you can't access local variables like that. Variables declared inside the function will not be accessible outside of it. And even if you could you would still not be getting what you expect because you're using $var2 and $var3 before initializing them.
What you're looking for is possible by passing functions around. For example:
function test( $var1 ) {
$var2 = 4;
$var3 = 5;
// this line calls the function named in $var1 with $var2 and $var3 as arguments
$value = call_user_func( $var1, $var2, $var3 );
// in later versions of PHP you can do this instead:
// $value = $var1( $var2, $var3 );
}
function addThem( $a, $b ) {
return $a + $b;
}
test( 'addThem' );
Notice that the method of combining variables is passed as a function (or at least, the closest PHP has to passing functions around; PHP is an odd language). Also the values it works on must be passed as parameters.
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.
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
);
?
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;