Suppose you have some strings, how should I transform them in order to be able to use logical operations on them in PHP? Is it even possible?
Example: I want
"x=1"&&"x=0"
to return false
.
Suppose you have some strings, how should I transform them in order to be able to use logical operations on them in PHP? Is it even possible?
Example: I want
"x=1"&&"x=0"
to return false
.
Introduction
I noticed you have both Logical Operator &&
and Assignment Operator in a string =
and you want to evaluate the assignment has a logical operator in a string. Seriously i don't know how you got here but this is very wrong
but for education purpose tag along
Breakdown
"x=1" && "x=0" = False
^ ^ ^
| | |
| |
X == 1 | |
| |
AND |
|
X == 0
The above expression would always be false
because X
can not be equal
to 0
and 1
at the same time
To be able to have such evaluation in PHP
you need to write your own function eg logicalString
where you can evaluate the expression with something like logicalString("x=1")
or logicalString("x=0")
Assumption
$x = 1; // Imagine value of X
Example 1 &&
// Start Evaluation with &&
if (logicalString("x=1") && logicalString("x=0")) {
echo "&& - True\n";
} else {
echo "&& - False\n";
}
Output 1
&& - False
Example 2 - ||
// Start Evaluation with ||
if (logicalString("x=1") || logicalString("x=0")) {
echo "|| - True\n";
} else {
echo "|| - False\n";
}
Output 2
|| - True
Function Used ( Not to be used in production See Why )
function logicalString($str) {
parse_str($str, $v);
foreach ( $v as $k => $var ) {
if (! isset($GLOBALS[$k]) || $GLOBALS[$k] != $var)
return false;
}
return true;
}
With the given information and let me explain what's happening ?
if("x=1"&&"x=0")
{
echo "true";
}
else
{
echo "false";
}
It will output
true
What you are trying achieve is not vaild. String is not a logical expression, logical expression consists of one or more logical operators and logical, numeric, or relational operands. Read more
What above code does is same as below
if("string1" && "string2")
{
echo "true";
}
else
{
echo "false";
}
Simply it does not care about what's in there. All it cares is there are two string which are not empty.
if("" && "")
{
echo "true";
}
else
{
echo "false";
}
above will return false
.
Conclusion
String is a always a string not a logical operator.
I think the best way to accomplish your task is to use a custom function trough eval() method like this:
<?php
function checkValues($var1, $var2) {
if ($var1==1 && $var2==1) {
echo "Both values are equal to 1";
} else {
echo "At least one value is not equal to 1";
}
}
$x = 1;
$y = 1;
$z = 2;
$z3 = 1;
eval("checkValues($x, $y);"); // Both values are equal to 1
eval("checkValues($x, $z);"); // At least one value is not equal to 1
/* The eval parameter is a string so you can play with it.
* Don't forget the semicolon ";";
*/
eval("checkValues($x, $z" . 3 . ");"); // Both values are equal to 1
?>
You can use this:
//Returns True if strings are equal, false otherwise
(bool)!strcmp($str1, $str2);
Wrap that in a function and you're set.