Jessica Posted January 16, 2007 Share Posted January 16, 2007 Basically, the goal is to loop through some numbers and run mathematical functions on them. I am trying to use eval to run the math, perhaps that isn't the right function...Here's what I've got:[code]$start = 1;$end = 4;$nums= range($start,$end);$ops = array('+', '-', '*', '/');$combos = array();foreach($nums AS $s1){ foreach($nums AS $s2){ foreach($nums AS $s3){ foreach($nums AS $s4){ $combos[] = array($s1, $s2, $s3, $s4); } } }}print '<p>There are '.count($combos).' combos for 4 digits containing '.$start.'-'.$end.'</p>';foreach($combos AS $c){ print '<b>'.$c[0].' '.$c[1].' '.$c[2].' '.$c[3].'</b><br />'; foreach($ops AS $o1){ foreach($ops AS $o2){ if($o2 != $o1){ foreach($ops AS $o3){ if($o3 != $o1 && $o3 != $o2){ $str = $c[0].' '.$o1.' '.$c[1].' '.$o2.' '.$c[2].' '.$o3.' '.$c[3]; print $str.' = '.eval($str.';').'<br />'; } } } } } }[/code]So here is some sample output from it:[quote]1 1 1 11 + 1 - 1 * 1 =1 + 1 - 1 / 1 =1 + 1 * 1 - 1 =1 + 1 * 1 / 1 =1 + 1 / 1 - 1 =1 + 1 / 1 * 1 =1 - 1 + 1 * 1 =1 - 1 + 1 / 1 =1 - 1 * 1 + 1 =1 - 1 * 1 / 1 =1 - 1 / 1 + 1 =1 - 1 / 1 * 1 =1 * 1 + 1 - 1 =1 * 1 + 1 / 1 =1 * 1 - 1 + 1 =1 * 1 - 1 / 1 =1 * 1 / 1 + 1 =1 * 1 / 1 - 1 =1 / 1 + 1 - 1 =1 / 1 + 1 * 1 =1 / 1 - 1 + 1 =1 / 1 - 1 * 1 =1 / 1 * 1 + 1 =1 / 1 * 1 - 1 = [/quote]So basically for the first one, I send the string '1 + 1 - 1 * 1' to eval and I expect to get the number 1 back. Instead of nothing after the = it should be printing the result of the math. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/34461-eval-returns-nothing/ Share on other sites More sharing options...
Jessica Posted January 16, 2007 Author Share Posted January 16, 2007 Found a function in the comments that does exactly what I want.http://us3.php.net/manual/en/function.eval.php#71045Nevermind ;) Quote Link to comment https://forums.phpfreaks.com/topic/34461-eval-returns-nothing/#findComment-162284 Share on other sites More sharing options...
sasa Posted January 16, 2007 Share Posted January 16, 2007 changeprint $str.' = '.eval($str.';').'<br />';toprint $str.' = '; eval('print '.$str.';'); print '<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/34461-eval-returns-nothing/#findComment-162373 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.