Convert string to variable in PHP - php

I have a variable $allrule = '(1 == 2) && (2 == 2)';
when i check if($allrule), it returns true because $allrule is treated as string. So i want to convert $allrule as condition of if statement. how can we do it.

This solution uses eval() which is pure evil, but since it wasn't stipulated not to do so...
$allrule = '(1 == 2) && (2 == 2)';
$result = eval("return (".$allrule.");"); // $result will be false
Expanded Example*:
$allrule = "(1433861812 > 1433694000) && (1433861812 > 1433771400) && (1433861812 > 1433944200)";
$result = eval("return (".$allrule.");");
if($result) {
echo "true";
} else {
echo "false"; // will echo "false"
}
*from comments

Related

Can I build an if expression ouside the if statement in PHP

Is it possible to build an if expression outside the if statement itself? Can I concatenate multiple conditions outside the statement and then test them in another if statement?
For example
$var= '$varCars==5 && varBikes == '.$totalBikes;
if(isset($trucks){
$var= $var.' && varTrucks=='.$totalTrucks';
}
if($var){
run some code
}
This was easy in VBScript!!
Harj
Is this what you are after?
$intA = 1;
$intB = 2;
// Returns boolean
$bool = ($intA === 1 && $intB === 2);
if($bool) echo "Yes, it is true<br>";
// if / else
$statement = ($intA === 1) ? 2 : 1;
if($statement === 2) echo "Yep, it is true.<br>";
// if / elseif / else
$statement = ($intA === 2) ? 2 : (($intB === 2) ? 2 : 1);
if($statement === 2) echo "Yep, intA is not equal to 2 but intB is, it is true.<br>";
// if / else concatenate in echo
echo "Hello ".(($intA === 2) ? "John" : "Jane")." do you want to grab some coffee?<br>";

How to check different conditions into if statement based on different scenario

I want to check different conditions into if statement based on different scenario (Will get the $status value as 'Y' or 'N'). Please check the code below and let me know the issue here.
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if($cond)
{
echo "Success";
}
This code is not working as it takes the "$fstrto <= $cstrto" as variable.
Remove the quotes. Use intval/doubleval if the input is a string as in $fstrto = intval($fstrto);.
$fstrto = 10;
$cstrto = 7;
if($status == 'N')
{
$cond = $fstrto <= $cstrto;
}
else
{
$cond = $fstrto >= $cstrto;
}
if($cond)
{
echo "Success";
}
Why it works: $cond is being assigned the value of a boolean expression, the values of which can be true or false. if($cond) just checks whether $cond is true or false
what is need to do is when using string as a php code use
eval — Evaluate a string as PHP code
Use below code work like charm:
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if(eval("return $cond;"))
{
echo "Success";
}
IMPORTANT:
Use of eval is highly discouraged
NEVER EVER use eval with params by POST/GET without sanitize them
When is eval evil in php?
You "$fstrto <= $cstrto" is a string now a compare statement.
$fstrto = "10";
$cstrto = "7";
if( ($status == 'N' && $fstrto <= $cstrto) || ($status != 'N' && $fstrto >= $cstrto) )
{
echo "Success";
}
Potentially turn it into a function that funnels into a switch statement like so:
function evaluateCondition($status, $a, $b) {
switch ($status) {
case 'Y':
return $a >= $b;
break;
case 'N':
return $a <= $b;
break;
default:
// Error Log. Unknown Status.
}
}
Any future addition can be appended onto the switch statement as necessary, if it gets more convoluted have each case return a separate function() to improve readability.
In terms of the current version you could use it like so:
$result = evaluateCondition('Y', 5, 6);
var_dump($result); // bool(false)
Hope that helps.
$fstrto = "10";
$cstrto = "7";
$cond = false;
if($status == 'N')
{
if($fstrto <= $cstrto){
$cond = true;
}
}
else
{
if($fstrto >= $cstrto){
$cond = false;
}
}
if($cond)
{
echo "Success";
}

when i test this code , it was not echo `8 > 3`? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
when i test this code , it was not echo 8 > 3 (it's will echo 1 = 1)?
i check my code are correct but why not echo real result?
<?PHP
$number = 8;
if ($number = '0')
{
echo $number." = 0";
}
elseif ($number = '1')
{
echo $number." = 1";
}
elseif ($number = '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Comparison in PHP is done via the == operator, not the = operator.
<?PHP
$number = 8;
if ($number == '0')
{
echo $number." = 0";
}
elseif ($number == '1')
{
echo $number." = 1";
}
elseif ($number == '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Try this:
<?PHP
$number = 8;
if ($number == 0)
{
echo $number." = 0";
}
elseif ($number == 1)
{
echo $number." = 1";
}
elseif ($number == 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
You were using assigning operator, instead of equal to operator, and secondly check with integer value instead of string.
The = operator assigns the value on the right hand side to the variable on the left.
if ($number = '0')
This sets $number to 0, which is a falsy value, so returns false.
elseif ($number = '1')
This sets $number to 1 which is a truthy value, which returns true. This is why your code outputs 1 = 1.
You need to instead use the == equality operator:
if ($number == '0')
...
elseif ($number == '1')
...
...
You are assigning a value to the variable $number, you'll need to use the == or better === to match the number against the value of the variable.
In this case you know $number is going to be an integer, it is best practice in PHP to compare as strict as possible, which is the === operator. You can use the == operator, however the variable will only be loosely matched at that point.
For example a boolean false will also match an if-statement containing if ($number == 0), which is not what you want.
Please check the documentation of PHP to see the full extend of comparison.
Such as the following:
<?PHP
$number = 8;
// See the triple equal sign here
if ($number === 0)
{
echo $number." = 0";
}
// and here
elseif ($number === 1)
{
echo $number." = 1";
}
// and here
elseif ($number === 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>

Weird PHP number issue

Here is the simple code:
$result = 0;
$result = $obj->delete($this_id);
echo "Result:" . $result;
var_dump($result);
if ( (int) $result < 0 || $result == null ) {
echo "Here" . $result;
var_dump($result);exit;
}
Here is the result:
Result:0int(0)
Here0int(0)
Its not supposed to enter into if block. Because $result is = 0. Not < 0.
Am I missing something or PHP handles this differently?
The comparison to null should be === instead of ==. Since null can evalulate to 0, the comparision evaluates (0 == null) = true
if ( (int) $result < 0 || $result === null ) {
See http://php.net/manual/en/language.operators.comparison.php for more information
It looks like the cast to an int type is making your variable test incorrectly against 0.
Replace if( (int) $result ... with if( $result ...
Its not the first validation which matches:
var_dump(0 == null); //true
var_dump(false == null); //true

Adding conditions dynamically in php if condtion

I am trying to add a condition dynamically in the if condition . But it is not working . Please help me to fix the issue.
I am trying a code like this
$day_difference = "some integer value";
if(sheduled_time == 'evening'){
$condition = '>';
}else{
$condition = '==';
}
then
if($day_difference.$condition. 0){
echo "something";
}else{
echo "h";
}
An alternative to gerald's solution; I would suggest that you use a function that validates the inputs using a switch-case operation:
function evaluate ($var1, $operator, $var2)
{
switch $operator
{
case: '<': return ($var1 < $var2);
case: '>': return ($var1 > $var2);
case: '==': return ($var1 == $var2);
}
return null;
}
What you need is the eval() method.
I.e.
$var1 = 11;
$var2 = 110;
$cond1 = '$var1 > $var2';
$cond2 = '$var1 < $var2';
if(eval("return $cond1;")){
echo $cond1;
}
if(eval("return $cond2;")){
echo $cond2;
}
As justly noted beneath, you should exercise the necessary precautions when using this method!
This is not the way to do this.
Just define a function which returns true if the desired conditions are met.
For example, we can define the function decide which receives two arguments, $day_difference and $scheduled_time:
function decide($day_difference, $scheduled_time)
{
if($scheduled_time == 'evening')
{
return $day_difference > 0;
}
else
{
return $day_difference == 0;
}
}
And use it like so:
if( decide($day_difference, $scheduled_time) )
{
echo "something";
}
else
{
echo "h";
}
according to your requirements this can be done using the PHP eval() function which i don't recommend using it only when necessary.
you can check When is eval evil in php?
you can use the below script instead:
if( $sheduled_time == 'evening' && $diff > 0 )
{
echo "This is the Evening and the Difference is Positive";
}
else if($diff == 0)
{
echo "This is not evening";
}
Thankyou for helping me solve my question
I solved this in another way
$day_difference = "some integer value";
$var1 = false ;
if($sheduled_time == 'evening_before'){
if($day_difference > 0 ){
$var1 = true ;
}
}else{
if($day_difference == 0 ){
$var1 = true ;
}
}
if($var1 === true){
echo "something";
}else{
echo "h";
}

Categories