php - Logical operations between strings -
suppose have strings, how should transform them in order able use logical operations on them in php? possible?
example: want
"x=1"&&"x=0"
to return false
.
introduction
i noticed have both logical operator &&
, assignment operator in string =
, want evaluate assignment has logical operator in string. don't know how got here very wrong
education purpose tag along
breakdown
"x=1" && "x=0" = false ^ ^ ^ | | | | | x == 1 | | | | , | | x == 0
the above expression false
because x
can not equal
0
, 1
@ same time
to able have such evaluation in php
need write own function eg logicalstring
can evaluate expression logicalstring("x=1")
or logicalstring("x=0")
assumption
$x = 1; // imagine value of x
example 1 &&
// start evaluation && if (logicalstring("x=1") && logicalstring("x=0")) { echo "&& - true\n"; } else { echo "&& - false\n"; }
output 1
&& - false
example 2 - ||
// start evaluation || if (logicalstring("x=1") || logicalstring("x=0")) { echo "|| - true\n"; } else { echo "|| - false\n"; }
output 2
|| - true
function used ( not used in production see why )
function logicalstring($str) { parse_str($str, $v); foreach ( $v $k => $var ) { if (! isset($globals[$k]) || $globals[$k] != $var) return false; } return true; }
Comments
Post a Comment