manohoo Posted July 29, 2011 Share Posted July 29, 2011 A foreach statement produces 3 strings per iteration: foreach($_POST as $key=>$value) { $str1 = "$"."val_"."$key = new validation ('$key', '$value', '$val[$key]', null);"; $str2 = "$"."val_"."$key"."->validate()"; $str3 = "$"."rs .= "."$"."val_"."$key"."->error;"; echo "$str1 <br />"; echo "$str2 <br />"; echo "$str3 <br />"; } Each iteration produces output as follows: $val_master_ARSAcctNo = new validation ('master_ARSAcctNo', 'PM25-00001', 'arsacctno_pmi', null); $val_master_ARSAcctNo->validate() $rs .= $val_master_ARSAcctNo->error; I want to execute each one of the strings using eval(), so far I've been unsuccessful, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/243200-eval-challenge/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2011 Share Posted July 29, 2011 Just use an array - foreach($_POST as $key=>$value) { $val[$key] = new validation ($key, $value, $val[$key], null); $val[$key]->validate(); $rs .= $val[$key]->error; } Also, by using eval, you will need to validate all the post keys and values you put into it to prevent php code injection into your script. Quote Link to comment https://forums.phpfreaks.com/topic/243200-eval-challenge/#findComment-1249080 Share on other sites More sharing options...
manohoo Posted July 29, 2011 Author Share Posted July 29, 2011 PFMaBiSmAd, simple, brilliant! Quote Link to comment https://forums.phpfreaks.com/topic/243200-eval-challenge/#findComment-1249091 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2011 Share Posted July 29, 2011 There's almost never ever a case where eval needs to or should be used. Quote Link to comment https://forums.phpfreaks.com/topic/243200-eval-challenge/#findComment-1249095 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.