2

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.

user1301428
  • 1,593
  • 3
  • 21
  • 52
  • 1
    You'll have to *parse* those strings and run code based on them. How arbitrary can those expressions be? What does `x=0` mean exactly? Assign `0` to `x`? Compare a variable `$x` to `0`? – deceze Jul 03 '12 at 12:27
  • @deceze the strings are logical expressions, so `x=0` means that the variable `x` has value `0` – user1301428 Jul 03 '12 at 12:36
  • can you give a more complex example? the current one can be solved with a simple `explode`, but i guess this won't be the case for all of your input. – l4mpi Jul 03 '12 at 12:48
  • 3
    @user1301428 Strings are strings and not logical expressions; `x = 0` isn't a logical expressions either, but a variable assignment. ("the strings are logical expressions, so x=0 means that the variable x has value 0" – that are totally different things…) If you wish to use strings as logical expressions, you'll need to use [`eval()`](http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php). But then again 'x=0' isn't valid PHP-code (missing the dollar-sign in front of the variable name). Maybe you should explain, why are you trying to evaluate strings as expressions. – feeela Jul 03 '12 at 12:49
  • ...and if you've missed that distinction, then you're not likely to understand the implications of using eval() and how to make it safe. – symcbean Jul 03 '12 at 12:59
  • 3
    feeela: 'strings are strings' - a php file is just a string too. his strings are just another formal language which can be parsed and evaluated as expressions. and while his example isn't valid php, it is valid python code... – l4mpi Jul 03 '12 at 13:15
  • @feeela I understand what you are saying, let me try to make things clearer: suppose that I have two strings, `"x=true || x=false"` and `"x=true"`. I would like to use the logical operators in PHP to evaluate the logical AND of the two strings, thus giving me the result `true` in this case. I want to do this because I need to store this information as strings, but I also need to perform logical operations on them. – user1301428 Jul 03 '12 at 21:24
  • @l4mpi sure, another example, besides the one given in the comment for feeela, could be `"!(x!=0&&y=1)"` and `"y=0"`. What I would like to do is evaluate the logical expression `!(x!=0&&y=1)&&y=0` – user1301428 Jul 03 '12 at 21:26
  • You'll probably need something like [this math parser (http://www.bestcode.com/html/math_parser_for_php.html)](http://www.bestcode.com/html/math_parser_for_php.html). [here is another one (https://simplemathparser.codeplex.com/)](https://simplemathparser.codeplex.com/) – Jon Hulka Jan 16 '13 at 23:06

4 Answers4

6

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;
}

SEE ONLINE DEMO on PHP 4.3.0 - 5.5.0alpha3

Community
  • 1
  • 1
Baba
  • 89,415
  • 27
  • 158
  • 212
2

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.

Techie
  • 42,101
  • 38
  • 144
  • 232
2

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
?>
Gabriel Lupu
  • 941
  • 9
  • 15
2

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.

TheRealKingK
  • 779
  • 6
  • 14