Replace eval() php in logical operations [duplicate]












0
















This question already has an answer here:




  • PHP : Custom error handler - handling parse & fatal errors

    6 answers



  • PHP eval and capturing errors (as much as possible)

    5 answers




I'm doing a logic validation like



$answer = eval("return ".$stringToValidate.";"); 


where $stringToValidate is a logic expression like 'a' == 'b' or 100 < 200.



The problem is when I introduce a invalid string like a == 'b' or 100 <<< 200.



I´m looking for a replacement for eval function or try-catch syntax error



I was trying using try catch like



try{
$answer = eval("return ".$stringToValidate.";");
}catch(Exception $e){
return $e->getMessage();
}


but didn't work



i expect the output true and false of eval function and a exception control for syntax error



EDIT:



i tryed the solutions of duplicated and have the same problem, using the try-catch or the function PHP eval and capturing errors (as much as possible)



specifically if use a expresion 10000 < 20000 < 30000 and get the textual error syntax error, unexpected '<'
i investigate the symfony expresion language tool https://symfony.com/doc/current/components/expression_language.html
but when the expresion fails here, throw false and i cannot diference a bad expresion and a expresion that was false



EDIT2: parse errors cannot be catched
http://php.net/manual/en/function.set-error-handler.php










share|improve this question















marked as duplicate by ceejayoz php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 14:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

    – ceejayoz
    Jan 2 at 14:49













  • A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

    – cmbuckley
    Jan 2 at 14:50













  • @cmbuckley Cheers, I've added that to the dupes list.

    – ceejayoz
    Jan 2 at 14:52











  • Thanks, I've just noticed the link to do that myself in future ;-)

    – cmbuckley
    Jan 2 at 14:54













  • @ceejayoz exist any form of capturing that parse error?

    – Johan Andrés Idárraga Villa
    Jan 2 at 16:30
















0
















This question already has an answer here:




  • PHP : Custom error handler - handling parse & fatal errors

    6 answers



  • PHP eval and capturing errors (as much as possible)

    5 answers




I'm doing a logic validation like



$answer = eval("return ".$stringToValidate.";"); 


where $stringToValidate is a logic expression like 'a' == 'b' or 100 < 200.



The problem is when I introduce a invalid string like a == 'b' or 100 <<< 200.



I´m looking for a replacement for eval function or try-catch syntax error



I was trying using try catch like



try{
$answer = eval("return ".$stringToValidate.";");
}catch(Exception $e){
return $e->getMessage();
}


but didn't work



i expect the output true and false of eval function and a exception control for syntax error



EDIT:



i tryed the solutions of duplicated and have the same problem, using the try-catch or the function PHP eval and capturing errors (as much as possible)



specifically if use a expresion 10000 < 20000 < 30000 and get the textual error syntax error, unexpected '<'
i investigate the symfony expresion language tool https://symfony.com/doc/current/components/expression_language.html
but when the expresion fails here, throw false and i cannot diference a bad expresion and a expresion that was false



EDIT2: parse errors cannot be catched
http://php.net/manual/en/function.set-error-handler.php










share|improve this question















marked as duplicate by ceejayoz php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 14:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

    – ceejayoz
    Jan 2 at 14:49













  • A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

    – cmbuckley
    Jan 2 at 14:50













  • @cmbuckley Cheers, I've added that to the dupes list.

    – ceejayoz
    Jan 2 at 14:52











  • Thanks, I've just noticed the link to do that myself in future ;-)

    – cmbuckley
    Jan 2 at 14:54













  • @ceejayoz exist any form of capturing that parse error?

    – Johan Andrés Idárraga Villa
    Jan 2 at 16:30














0












0








0









This question already has an answer here:




  • PHP : Custom error handler - handling parse & fatal errors

    6 answers



  • PHP eval and capturing errors (as much as possible)

    5 answers




I'm doing a logic validation like



$answer = eval("return ".$stringToValidate.";"); 


where $stringToValidate is a logic expression like 'a' == 'b' or 100 < 200.



The problem is when I introduce a invalid string like a == 'b' or 100 <<< 200.



I´m looking for a replacement for eval function or try-catch syntax error



I was trying using try catch like



try{
$answer = eval("return ".$stringToValidate.";");
}catch(Exception $e){
return $e->getMessage();
}


but didn't work



i expect the output true and false of eval function and a exception control for syntax error



EDIT:



i tryed the solutions of duplicated and have the same problem, using the try-catch or the function PHP eval and capturing errors (as much as possible)



specifically if use a expresion 10000 < 20000 < 30000 and get the textual error syntax error, unexpected '<'
i investigate the symfony expresion language tool https://symfony.com/doc/current/components/expression_language.html
but when the expresion fails here, throw false and i cannot diference a bad expresion and a expresion that was false



EDIT2: parse errors cannot be catched
http://php.net/manual/en/function.set-error-handler.php










share|improve this question

















This question already has an answer here:




  • PHP : Custom error handler - handling parse & fatal errors

    6 answers



  • PHP eval and capturing errors (as much as possible)

    5 answers




I'm doing a logic validation like



$answer = eval("return ".$stringToValidate.";"); 


where $stringToValidate is a logic expression like 'a' == 'b' or 100 < 200.



The problem is when I introduce a invalid string like a == 'b' or 100 <<< 200.



I´m looking for a replacement for eval function or try-catch syntax error



I was trying using try catch like



try{
$answer = eval("return ".$stringToValidate.";");
}catch(Exception $e){
return $e->getMessage();
}


but didn't work



i expect the output true and false of eval function and a exception control for syntax error



EDIT:



i tryed the solutions of duplicated and have the same problem, using the try-catch or the function PHP eval and capturing errors (as much as possible)



specifically if use a expresion 10000 < 20000 < 30000 and get the textual error syntax error, unexpected '<'
i investigate the symfony expresion language tool https://symfony.com/doc/current/components/expression_language.html
but when the expresion fails here, throw false and i cannot diference a bad expresion and a expresion that was false



EDIT2: parse errors cannot be catched
http://php.net/manual/en/function.set-error-handler.php





This question already has an answer here:




  • PHP : Custom error handler - handling parse & fatal errors

    6 answers



  • PHP eval and capturing errors (as much as possible)

    5 answers








php laravel eval






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:37







Johan Andrés Idárraga Villa

















asked Jan 2 at 14:47









Johan Andrés Idárraga VillaJohan Andrés Idárraga Villa

135




135




marked as duplicate by ceejayoz php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 14:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by ceejayoz php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 14:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

    – ceejayoz
    Jan 2 at 14:49













  • A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

    – cmbuckley
    Jan 2 at 14:50













  • @cmbuckley Cheers, I've added that to the dupes list.

    – ceejayoz
    Jan 2 at 14:52











  • Thanks, I've just noticed the link to do that myself in future ;-)

    – cmbuckley
    Jan 2 at 14:54













  • @ceejayoz exist any form of capturing that parse error?

    – Johan Andrés Idárraga Villa
    Jan 2 at 16:30



















  • a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

    – ceejayoz
    Jan 2 at 14:49













  • A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

    – cmbuckley
    Jan 2 at 14:50













  • @cmbuckley Cheers, I've added that to the dupes list.

    – ceejayoz
    Jan 2 at 14:52











  • Thanks, I've just noticed the link to do that myself in future ;-)

    – cmbuckley
    Jan 2 at 14:54













  • @ceejayoz exist any form of capturing that parse error?

    – Johan Andrés Idárraga Villa
    Jan 2 at 16:30

















a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

– ceejayoz
Jan 2 at 14:49







a == 'b' generates a warning, not an exception, when I try it. 100 <<< 200 generates a parse error, which you can't catch.

– ceejayoz
Jan 2 at 14:49















A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

– cmbuckley
Jan 2 at 14:50







A better duplicate: PHP eval and capturing errors (as much as possible) - TL;DR use PHP7 and catch ParseError.

– cmbuckley
Jan 2 at 14:50















@cmbuckley Cheers, I've added that to the dupes list.

– ceejayoz
Jan 2 at 14:52





@cmbuckley Cheers, I've added that to the dupes list.

– ceejayoz
Jan 2 at 14:52













Thanks, I've just noticed the link to do that myself in future ;-)

– cmbuckley
Jan 2 at 14:54







Thanks, I've just noticed the link to do that myself in future ;-)

– cmbuckley
Jan 2 at 14:54















@ceejayoz exist any form of capturing that parse error?

– Johan Andrés Idárraga Villa
Jan 2 at 16:30





@ceejayoz exist any form of capturing that parse error?

– Johan Andrés Idárraga Villa
Jan 2 at 16:30












1 Answer
1






active

oldest

votes


















1














If you use PHP 7, you can use ParserError :



error_reporting(E_ALL);



$stringToValidate = "'a == 'b'";

try {
$answer = eval("return ".$stringToValidate.";");
} catch(Exception $e){
return $e->getMessage();
} catch (ParseError $e) {
echo 'Bad request !';
}


Edit : see cmbuckley comment






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you use PHP 7, you can use ParserError :



    error_reporting(E_ALL);



    $stringToValidate = "'a == 'b'";

    try {
    $answer = eval("return ".$stringToValidate.";");
    } catch(Exception $e){
    return $e->getMessage();
    } catch (ParseError $e) {
    echo 'Bad request !';
    }


    Edit : see cmbuckley comment






    share|improve this answer




























      1














      If you use PHP 7, you can use ParserError :



      error_reporting(E_ALL);



      $stringToValidate = "'a == 'b'";

      try {
      $answer = eval("return ".$stringToValidate.";");
      } catch(Exception $e){
      return $e->getMessage();
      } catch (ParseError $e) {
      echo 'Bad request !';
      }


      Edit : see cmbuckley comment






      share|improve this answer


























        1












        1








        1







        If you use PHP 7, you can use ParserError :



        error_reporting(E_ALL);



        $stringToValidate = "'a == 'b'";

        try {
        $answer = eval("return ".$stringToValidate.";");
        } catch(Exception $e){
        return $e->getMessage();
        } catch (ParseError $e) {
        echo 'Bad request !';
        }


        Edit : see cmbuckley comment






        share|improve this answer













        If you use PHP 7, you can use ParserError :



        error_reporting(E_ALL);



        $stringToValidate = "'a == 'b'";

        try {
        $answer = eval("return ".$stringToValidate.";");
        } catch(Exception $e){
        return $e->getMessage();
        } catch (ParseError $e) {
        echo 'Bad request !';
        }


        Edit : see cmbuckley comment







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 14:52









        Vincent DecauxVincent Decaux

        4,19132132




        4,19132132

















            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas