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? 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. 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! 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. Link to comment https://forums.phpfreaks.com/topic/243200-eval-challenge/#findComment-1249095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.