Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Division and Eval()

Status
Not open for further replies.

likelylad

IS-IT--Management
Joined
Jul 4, 2002
Messages
388
Location
GB
I am dynamically producing a query from a table to run on another table.

The bulk of this is no problem.

The problem I am having to get 2 variables to divide.

For Example
Code:
$rowdata[$i]='$B'.$myrow["Num"].'$A'.$myrow["Num"];

Further On down in the code I use:
Code:
for($a=0;$a<$numcols;$a++){
$out="<td>rowdata[$a]</td>";
eval("\$out = \"$out\";");
echo $out;
}


This could output 1020 (i.e. 10 and 20),which is fine.

However I would like to divide these 2 numbers (i.e. 10/20).

The best I have got is to have 10/20 displayed on screen rather than the actual division result.

Hopefully the above is clear?

Does anyone have any ideas?????
 
I don't understand. Why are you using an eval()? Why not just have the code divide one provided number by another?



Want the best answers? Ask the best questions! TANSTAAFL!
 
The formulae is being passed as a string so the only way to get it to display the actual variable is to use eval()
 
Your example confuses me, as $myrow['Num'] has two values in the same statement.

Here's something similar:

Code:
<?php

$a = array
(
	array ('10', '20', '/'),
	array ('10', '20', '*'),
	array ('10', '20', '-'),
	array ('10', '20', '+')
);
	
foreach ($a as $operands)
{
	$string = '$out = ' . $operands[0] . $operands[2] . $operands[1] . ';';
	
	print $string . ' : ';
	eval ($string);
	print $out;
	print '<br>';
}

On my machine it outputs:

[tt]$out = 10/20; : 0.5
$out = 10*20; : 200
$out = 10-20; : -10
$out = 10+20; : 30[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
The first part of the code will produce a string like

$rowdata[1]=$Bvar1.$Avar3;
$rowdata[2]=$Bvar2.$Avar3;
$rowdata[3]=$Bvar3.$Avar3;
etc

This then needs to be evaluated in the second part of the code
 
I think my code sample shows you how to construct evaluatable strings from parameter-driven inputs. I don't understand your last post.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top