EQUATION IN PHP PLZ HELP...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

EQUATION IN PHP PLZ HELP...

Post by amirbwb »

hi, my point is to make a programme with php, so here we go:
you put an equation in ONE feild and it will solve it to you
here is a simple example:
<?php
$eq=$_POST['eq'];
?>

<form name="equation" method="POST" action="index.php">
<input type="text" name="eq" id="eq" value="<?php echo $eq;?>" >
<input type="submit" name="submit" value="SOLVE" >
</form>


<?php echo $eq;?> // if I put this line it will show me(lets say that i entered in the feild "1+2") :-----------------> it will show me "1+2", not "3"(the result)

NB : I don't want to use 2 feilds with 2 variables $a=$_POST['a']; $b=$_POST['b']; $result=$a+$b because the "+" can be chaged "/" "*" "-"

/////////////////////////////////////
another example:

[2+1__________________] [submit]

Result:
2+1

//////////////////////////////// I WANT THE RESULT TO BE "3"

THANK YOU
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: EQUATION IN PHP PLZ HELP...

Post by yacahuma »

yoiu could use this but not in a production environment

Code: Select all

  <form action="test2.php" method="post">
  <input type="text" name="eq" />
  <input type="submit" name="submit" />
  </form>
  
  <?
    if (isset($_POST['submit']))
    {
       eval("\$res = {$_POST['eq']};");    
       echo $_POST['eq']  . ' = ' . $res;
    }
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: EQUATION IN PHP PLZ HELP...

Post by califdon »

The trouble with answering questions like this is that the OP is apparently trying to learn about PHP programming, but they are asking questions that bear little relation to anything useful that they are likely to ever need to do. As a former teacher, it is my opinion that this is a practice that does NOT lead to learning anything useful. If I understand the OP's question, it has little value in the real world of PHP applications and is thus counterproductive for the OP. If the OP has some other goal in mind, they would be well advised to explain what they're really trying to do. Otherwise it is just a waste of time for all of us, including the OP.
Placebo
Forum Newbie
Posts: 2
Joined: Sun Oct 31, 2010 8:30 am
Location: uk,Norfolk

Re: EQUATION IN PHP PLZ HELP...

Post by Placebo »

basically its a calculator... surely you best off having three field in your form, you can find them on google easily enough just have put in search simple php calculator
Post Reply