Jump to content

Eval returns nothing?


Jessica

Recommended Posts

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 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 * 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?
Link to comment
https://forums.phpfreaks.com/topic/34461-eval-returns-nothing/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.