### Thread: solving algebra with php ?

Page 1 of 3 123 Last
1. #### solving algebra with php ?

hi .. i am not so new to php ... i have been trying to learn php for some time now ...

learned few important basic things and stuffs about programming in general ...ok

finally narrowed it down to solving a few algebra questions ...in php ...

hello user .... input a algebras part ?
give it to the formula
computer .. display the result ...

2. It'd help if you can actually state the problem you're trying to solve.

Is it that you have an equation like "3x^2-5x^3+x+m", and values like "m=0.1", and limits for x like -1 to 1, and you need to find values of x where the equation is zero?

• Will-O-The-Wisp agrees
3. it doesnt exactly have to be this formula that i am trying to solve ...

i am mostly intrested in ... some simple algebra equations ... where i can make the program prompt for a user input ... ? is it possible ? i am also looking for such a simple algebra equation with which i can do that ...

some algebra equation ...

hello user .... input a algebras part ?
give it to the formula
computer .. display the result ...
4. Yeah, I know it's not that exact formula. I mean, is the formula something like where you have one unknown variable, maybe have one or more other known variables, and you have to solve for the value of that variable within some known range?

If that's it, the first step is parsing the formula so you can represent it in code. Probably the best way is with a sort of state machine: you run through the equation character by character, keeping track of all sorts of things as you go, such that you can rebuild the formula in code. You'll probably end up with a tree structure where each node in the tree represents an operation, like multiplication or addition, which you can "walk" to evaluate the expression.

Do you have any experience with anything like this? It's actually quite a complicated subject.

• Will-O-The-Wisp agrees
5. maybe i should find such a simple question from somewhere ... where i can solve something from a given formula... a given equation ... a given linear equation ... a non linear equation ... a matrix equation ... or a mathematical function ...

we had computer oriented numerical method in college ... which dealt with things like these... i had a very hard time understanding things like those especially since everything was in c ...

i did not know which part to focus ... the language syntax or the equation themselves ...

its almost like i had to come up with extra stuffs like ... countf the percentage of elements , things like that ...

then i decided to refresh all these things in php... like

start counting the values of x ( countf the percentage of elements ) ...

<?php
\$txt = "Hello world!";
\$x = 5;
\$y = 10.5;
?>

things like that ...

or an even simple one like , enter \$x =
\$y =

i dont have edit previlages right now as a user on the threads i make ?
6. No Profile Picture
Contributing User
Devshed Specialist (4000 - 4499 posts)

Join Date
Jul 2003
Posts
4,473
Rep Power
653
It sounds like you are looking for a Recursive Descent Parser algorithm.
7. no ... not that deep ...

something simple ... like when the program starts ... " hello user enter x "

when do i get to edit my own threads ?

• Will-O-The-Wisp agrees : You may need to refresh your browser for the "edit post" link to appear. There will be a button just below each of your posts, with a little pencil next to it.
8. No Profile Picture
Contributing User
Devshed Specialist (4000 - 4499 posts)

Join Date
Jul 2003
Posts
4,473
Rep Power
653
OK, keep in mind that PHP is server side only and is stateless. That means once you output the page, PHP forgets everything and does not know what is happening on the client side. So to do what you want you create a form and perhaps this will help you get started.

P.S. Please read the sticky at the top of this forum that says READ THIS BEFORE POSTING.

• Will-O-The-Wisp agrees
Last edited by gw1500se; August 14th, 2015 at 08:21 AM.
9. i found a thread like this from somewhere...
How to take user input in php, like we take it in c/c++?
How to take user input in php, like we take it in c/c++, I am writing a program on codechef, and trying to write it in php but not able to get user input please help.!!
PHP Code:
``` <?php if(isset(\$_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */ { \$var = \$_POST['any_name'];   // Here \$var is the input taken from user. } ?> <form action="<?php echo \$_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="any_name"> <input type="submit" name="submit"> </form> ```
PHP Code:
``` <?php \$temp = fopen ("php://stdin","r"); \$line = fgets(\$temp); echo \$line; ?> ```
why is something like this not mentioned in w3schools php tutorial ?
10. No Profile Picture
Contributing User
Devshed Specialist (4000 - 4499 posts)

Join Date
Jul 2003
Posts
4,473
Rep Power
653
Because W3schools sucks and should not be used for anything. It is obsolete and just flat out wrong in many cases.
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2015
Posts
9
Rep Power
0
I think you should define a set of requirements first. Depending on what exactly you want the program to do, this can be a simple or a complex task

1. collect input
2. solve equation
3. output

is way too general.

* Do you have PHP installed on your system? if yes, which version?
* Do you want to run the PHP through the command like (like c etc,) or a web-server with a browser?
13. No Profile Picture
Super Moderator
Devshed Intermediate (1500 - 1999 posts)

Join Date
Jun 2009
Location
Hartford, WI
Posts
1,569
Rep Power
131
Do you have any script started already? For the sake of starting a VERY basic version of this script, simply to get it started: Provide a form to request 2 numbers, then upon submission, have PHP add those 2 together and echo the answer in the desired area. This will get the initial script laid out. From here, you can then alter things here and there as desired.

This example you provided is relatively all you need to start this. Just edit it as needed...
PHP Code:
``` <?php if(isset(\$_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */ { \$var = \$_POST['any_name'];   // Here \$var is the input taken from user. } ?>  <form action="<?php echo \$_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="any_name"> <input type="submit" name="submit"> </form> ```
14. well to the very basic of adding two numbers ...i found few examples from different places ...
PHP Code:
``` <?php        \$value1 = \$_POST['value1'];        \$value2 = \$_POST['value2']; ?> <html>        <title>PHP - Add two numbers</title>        <body>               <form action="addtwonumbers.php" method="post">                      <input type="text" name="value1" value="0" />                      <input type="text" name="value2" value="0" />                      <input type="submit" value="Calculate values"/>               </form>               Answer : <?php echo (\$value1 + \$value2); ?>        </body> </html> ```
To be able to read input from the script’s user, you can use STDIN combined with fgets(), fread(), fscanf() or fgetc(). For example:

PHP Code:
``` <?php fwrite(STDOUT, "Please enter your namen");  // Read the input \$name = fgets(STDIN); fwrite(STDOUT, “Hello \$name”); // Exit correctly exit(0); ?> ```

PHP Code:
``` <?php  echo "Please enter value 1 : ";  fscanf(STDIN, "%d\n", \$value1); // reads number from STDIN standard input  echo "Please enter value 2 : ";  fscanf(STDIN, "%d\n", \$value2);  echo "Answer : " .(\$value1 + \$value2) . "\n"; ?> ```
15. No Profile Picture
Super Moderator
Devshed Intermediate (1500 - 1999 posts)

Join Date
Jun 2009
Location
Hartford, WI
Posts
1,569
Rep Power
131
hi .. i am not so new to php
I believe you might be jumping a bit too far ahead of yourself if you couldn't just toss together this basic addition script for us...

This is a quick script off the top of my head in maybe a minute. More can be added to it, such as checks to make sure the items entered are numeric and all, but this is a basic quick toss-together...

PHP Code:
``` <?PHP   \$sum = "";   if(isset(\$_POST['num1']) && isset(\$_POST['num2'])) {     \$sum = \$_POST['num1'] + \$_POST['num2'];   } ?> <FORM action="" method="post">   Number 1: <INPUT name="num1" type="text" /><BR />   Number 2: <INPUT name="num2" type="text" /><BR />   <INPUT type="submit"> </FORM> <BR />Answer: <DIV id="answer"><?= \$sum; ?></DIV> ```
From this, to achieve the level you provided in your first post, won't take much alteration/addition to this script...