Jump to content

A variable that is a math equation- how to make it DO the equation?


kernelgpf

Recommended Posts

I'm trying to get a number, and have this code..

 

$firetemp="100$sign[1]$random[1]";

 

Now- don't worry about the $sign or $random arrays, those are working perfectly. The problem is, the result is $firetemp equals something like "100-16" or "100+2", when I actually need the RESULT of the equation- like, right now it might give me "100+2", but I need it to give me "102". How can I do this?

 

Thanks!

Link to comment
Share on other sites

whoops my bad. forgot to escape the first $ (note it's now \$result instead of just $result):

 

<?php
// slight mod to your string
$firetemp="\$result = 100{$sign[1]}{$random[1]};";
eval($firetemp);

echo $result;
?>

 

also, it's not always essential but a good habit to get into when enclosing vars in a string to be parsed, to include them within curly braces {}. this makes sure that there's no clashes between the var name and any surrounding text.

Link to comment
Share on other sites

Okay- I tried that, I'm getting lots of errors. Here's my code-

 

$airtemp="\$result = 60{$sign[0]}{$random[0]};";
$firetemp="\$result2 = 100{$sign[1]}{$random[1]};";
$watertemp="\$result3 = 70{$sign[2]}{$random[2]};";
$icetemp="\$result4 = 30{$sign[3]}{$random[3]};";
$dvtemp="\$result5 = 70{$sign[4]}{$random[4]};";
$lighttemp="\$result6 = 80{$sign[5]}{$random[5]};";
$poisontemp="\$result7 = 90{$sign[6]}{$random[6]};";
$earthtemp="\$result8 = 70{$sign[7]}{$random[7]};";

eval($airtemp);
eval($firetemp);
eval($watertemp);
eval($icetemp);
eval($dvtemp);
eval($lighttemp);
eval($poisontemp);
eval($earthtemp);

mysql_query("update clubs set temperature='$result' where district='Air District'");
mysql_query("update clubs set temperature='$result2' where district='Fire District'");
mysql_query("update clubs set temperature='$result3' where district='Water District'");
mysql_query("update clubs set temperature='$result4' where district='Ice District'");
mysql_query("update clubs set temperature='$result5' where district='Dragon Valley District'");
mysql_query("update clubs set temperature='$result6' where district='Light District'");
mysql_query("update clubs set temperature='$result7' where district='Poison District'");
mysql_query("update clubs set temperature='$result8' where district='Earth District'");

 

And I'm getting these errors-

 

 

Parse error: syntax error, unexpected T_LNUMBER in /home/colrob/public_html/crons4246/dailies2.php(41) : eval()'d code on line 1

 

Parse error: syntax error, unexpected T_LNUMBER in /home/colrob/public_html/crons4246/dailies2.php(42) : eval()'d code on line 1

 

Parse error: syntax error, unexpected T_LNUMBER in /home/colrob/public_html/crons4246/dailies2.php(43) : eval()'d code on line 1

 

Parse error: syntax error, unexpected T_LNUMBER in /home/colrob/public_html/crons4246/dailies2.php(44) : eval()'d code on line 1

 

Parse error: syntax error, unexpected T_LNUMBER in /home/colrob/public_html/crons4246/dailies2.php(45) : eval()'d code on line 1

Link to comment
Share on other sites

can you post some example data or even the code that sets up $sign and $random ?

 

edit: this works for me:

 

// test data in arrays
$sign = array('*');
$random = array(rand(50,100));

$firetemp="\$result = 100{$sign[0]}{$random[0]};";
eval($firetemp);
echo $result;

Link to comment
Share on other sites

Am I missing the whole point?

 

$airtemp="\$result = 60{$sign[0]}{$random[0]};";

 

That just looks like a really ugly way to 'calculate' something. Do you actually care what $result is, or do you really want $airtemp to be the calculated result - in which case, try this:

 

$airtemp = 60 * $sign[0] * $random[0];

Link to comment
Share on other sites

<?php
include 'config.php';
$month=date('F');
if($month == 'April' || $month == 'May' || $month == 'June' || $month == 'July' || $month == 'August' || $month == 'September' || $month == 'October'){
//summer!
for($i=0;$i<7;$i++){
$random2=rand(0,30);
$random3=rand(0,1);
if($random3 == '0'){
$sign="+";
}
else{
$sign="-";
}

$random[$i]=$random2;
$sign[$i]=$sign;
}
$airtemp="\$result = 60{$sign[0]}{$random[0]};";
$firetemp="\$result2 = 100{$sign[1]}{$random[1]};";
$watertemp="\$result3 = 70{$sign[2]}{$random[2]};";
$icetemp="\$result4 = 30{$sign[3]}{$random[3]};";
$dvtemp="\$result5 = 70{$sign[4]}{$random[4]};";
$lighttemp="\$result6 = 80{$sign[5]}{$random[5]};";
$poisontemp="\$result7 = 90{$sign[6]}{$random[6]};";
$earthtemp="\$result8 = 70{$sign[7]}{$random[7]};";
}
else{
//winter!
$airtemp="\$result = 50{$sign[0]}{$random[0]};";
$firetemp="\$result2 = 80{$sign[1]}{$random[1]};";
$watertemp="\$result3 = 40{$sign[2]}{$random[2]};";
$icetemp="\$result4 = 10{$sign[3]}{$random[3]};";
$dvtemp="\$result5 = 40{$sign[4]}{$random[4]};";
$lighttemp="\$result6 = 50{$sign[5]}{$random[5]};";
$poisontemp="\$result7 = 70{$sign[6]}{$random[6]};";
$earthtemp="\$result8 = 50{$sign[7]}{$random[7]};";
}

eval($airtemp);
eval($firetemp);
eval($watertemp);
eval($icetemp);
eval($dvtemp);
eval($lighttemp);
eval($poisontemp);
eval($earthtemp);

mysql_query("update clubs set temperature='$result' where district='Air District'");
mysql_query("update clubs set temperature='$result2' where district='Fire District'");
mysql_query("update clubs set temperature='$result3' where district='Water District'");
mysql_query("update clubs set temperature='$result4' where district='Ice District'");
mysql_query("update clubs set temperature='$result5' where district='Dragon Valley District'");
mysql_query("update clubs set temperature='$result6' where district='Light District'");
mysql_query("update clubs set temperature='$result7' where district='Poison District'");
mysql_query("update clubs set temperature='$result8' where district='Earth District'");

?>

 

It's not calculating something- the $sign array is either a plus or minus sign. That's my entire script.

Link to comment
Share on other sites

try

<?php
include 'config.php';
$month=date('F');
$month_arr = array('April', 'May', 'June', 'July', 'August');
$district_arr = array(
'Air District' => 60, 
'Fire District' => 100, 
'Water District' => 70, 
'Ice District' => 30, 
'Dragon Valley District' => 70, 
'Light District' => 80, 
'Poison District' => 90, 
'Earth District' => 70
);
if(in_array($month, $month_arr)){
//summer!
foreach ($district_arr as $district => $value){
	$random = rand(0, 30) * (rand(0,1) * 2 - 1);
	// or use  just $random = rand(-30, 30);
	$result = $value + $random;
	print("update clubs set temperature='$result' where district='$district'"); // change print to mysql_query()
}
}
?>

sorry i update code

Link to comment
Share on other sites

As Andy suggested:

 

<?php

include 'config.php';

$month=date('F');
if($month == 'April' || $month == 'May' || $month == 'June' || $month == 'July' || $month == 'August' || $month == 'September' || $month == 'October')
{
//summer!
for($i=0;$i<7;$i++)
{
	$random2 = rand(0,30);
	$random3 = rand(0,1);
	if($random3 == '0')
		$sign = 1;
	else
		$sign = -1;

	$random[$i] = $random2;
	$sign[$i] = $sign;
}
$result = 60 * $sign[0] + $random[0];
}

mysql_query("update clubs set temperature='$result' where district='Air District'");

?>

 

 

Orio.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.