/    Sign up×
Community /Pin to ProfileBookmark

eval() function

Revisiting this calculator script, now this is what i have:

[code]
<?php

$hist = $_POST[‘hist’];
$calc = $_POST[‘calc’];
eval(“$calc2 = “$calc”;”);
$hist = $hist . $calc2. ” = ” . $calc . “<br>”;
echo $hist ;

?>

<form method=”post” action=”index.html”>
<input type=”hidden” name=”hist” value=”<?php echo($hist); ?>”>
<input type=”text” name=”calc” size=”50″><br>
<input type=”submit” value=”solve”>
</form>
[/code]

Almost works, except it doesn’t actually eval the problem, it just spits it back out as a string. There is something wrong in the eval parameter. This is crazy, this function is SOOOOOOOOOOO weird! I keep playing around with the syntax, but the only one that wont give me an error is this one (taken from php.net manual)

Anyhu, any help with this? I think all the rest of the script is perfect, I just need to get the parameter in that eval function notated correctly. PLEASE HELP!

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 04.2005 — [code=php]eval("$calc2 = $calc;");[/code]
Copy linkTweet thisAlerts:
@gameguy43authorMar 04.2005 — nope, that gives me an error message
Copy linkTweet thisAlerts:
@SpectreReturnsMar 04.2005 — [code=php]
eval('$calc2 = '.$calc);
[/code]
Copy linkTweet thisAlerts:
@gameguy43authorMar 04.2005 — i know before even looking at that one that it doesnt work, come on people, focus! I already said that this is a tricky function, pls double-check before you post. This function requires some weird notation, it'c not as easy as you think.
Copy linkTweet thisAlerts:
@SpectreReturnsMar 04.2005 — That should work. The eval function acts as normal PHP. But, I do see a problem with that.

[code=php]
eval('$calc2 = '.$calc.';');
[/code]


Can you give some examples of what is being sent to $calc, or what errors you're getting?
Copy linkTweet thisAlerts:
@PSLohMar 04.2005 — come on man, i understand ur frustration, but ppl in this forum are really helpful n they really try to help...

Try:

eval("$calc2 = "$calc";")
Copy linkTweet thisAlerts:
@SpectreReturnsMar 04.2005 — [i]Originally posted by PSLoh [/i]

[B]come on man, i understand ur frustration, but ppl in this forum are really helpful n they really try to help...



Try:

eval("$calc2 = "$calc";") [/B]
[/QUOTE]

If dealing with numbers, that will make PHP think it's trying to do:
[code=php]
$calc2 = "$calc";
[/code]

So $calc will become a string, but this guy is trying to do number operations, so it will become "3+6" instead of 9.
Copy linkTweet thisAlerts:
@PSLohMar 04.2005 — u r right Spec...
Copy linkTweet thisAlerts:
@NogDogMar 04.2005 — [i]Originally posted by gameguy43 [/i]

[B]nope, that gives me an error message [/B][/QUOTE]

Sure does, but not because the eval() syntax is wrong, but because $calc is empty when you first access the page. This works just fine:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Calculator</title>
<style type="text/css">
<!--
body {
background-color: white;
font: medium arial, helvetica, sans-serif;
}
div {
width: 44em;
margin: 1em auto;
padding: 1em;
}
#hist {
float: right;
width: 15em;
margin: 0 0 0 2em;
padding: 1em;
border: solid thin gray;
}
-->
</style>
</head>
<body>
<div>
<h1 style="text-align: center;">PHP Calculator</h1>
<p id=hist>
<?php
if(isset($_POST['calc']))
{
$hist = $_POST['hist'];
$calc = $_POST['calc'];
eval("$calc2 = $calc;");
$hist = "$hist$calc = $calc2<br>";
echo $hist ;
}
?>
</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p>Enter calculation:<br>
<input type="hidden" name="hist" value="<?php echo($hist); ?>">
<input type="text" name="calc" size="50"><br>
<input type="submit" value="solve"></p>
</form>
</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@gameguy43authorMar 04.2005 — yeh, i just figured that out and was about to post to apologize for saying u guys were wrong when i saw that post. Sorry guys! source now works and looks like this, thanks for all of your help! I'll be looking at ading some security stuff in there, so i may be posting about this script yet again soon ?
[code=php]
<?php

if($hist != ""){
$hist = $_POST['hist'];
$calc = $_POST['calc'];
eval("$result=" . $calc . ";");
$hist = $hist . $calc. " = " . $result . "<br>";
echo $hist ;
}
else $hist = "<!--hi dood-->";

?>

[/code]

lol, not the neatest solution, but it works, thanks again and sorry i went balistic, was getting really frustrated...
×

Success!

Help @gameguy43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...