Jump to content
xisto Community
alex1985

Php Code? Mathematical Applications

Recommended Posts

Hello, everyone. The help is needed again.

 

How can I make calculator in PHP language? That will act like that a user just type in the fields known values, then click the button, and it's going to be solved automatically. In other words, have can I write a formula in PHP, how to plug it inside that language. For example, the formula to find a peremeter of square is:

 

P=4a.

 

So, a user just can write the known value which is peremeter itself and it will find the side of a square; and vice versa.

 

If you can write many things how to do such formulas, such as complicated ones.

Share this post


Link to post
Share on other sites

Here is a basic php math tutorial, that unfortunately I did not write.... and your going to have to play with it alot... but with some work, you should be able to figure it out.....

Equations in PHP can be easily executed. Just like in real life, you can add, subtract, multiply, or divide. How, you have to ensure you have done everything correctly to make a successful equation. For example, 5x-3x would not work. But, trying to make something like 5-3 would work. How exactly? I'll show you.
Code
<?php
$num[1]= 5;
$num[2] = 3;
$total = $num[1] - $num[2];
echo "The number is $total";
?>
Outcome
The number is 3

That is what I would consider a very basic equation. But, you can get much more complicated. How much more? Well, by having a couple sets of brackets and more numbers, you can make yourself look like an PHP equation wiz. Here is one of my text equations I tried while playing around with PHP.

Code
<?php
$num[1] = 5;
$num[2] = 3;
$num[3] = 2;
$num[4] = 2;
$total = (($num[1]+$num[2])/$num[3])*$num[4];
echo "[ ( $num[1] + $num[2] ) / $num[3] ] * $num[4] = $total";
?>
Outcome
[ ( 5 + 3 ) / 2 ] * 2 = 8

Now, you may be asking yourself, "But don't you use an "x" sign to multiply numbers?". Well, no. You see, in PHP programming, if you try to multiply two numbers with an "x", you will just come up with a parse error, simple as that. So, instead, you just have to put in a star. The same goes for dividing. Should you try to divide by using a division sign, you will also come up with an error. Instead, you have to use a slash.

When doing equations in PHP, the programming also uses the acronym of BEDMAS to do the question. In case you don't know, BEDMAS is the form of doing questions in a certain order if multiple operations were required. So it goes, Brackets, Exponents, Division, Multiplacation, Addition, Subtraction. So if you want a certain part to be done first, you must put brackets around it. So let's review the equation I made earlier.

[ ( 5 + 3 ) / 2 ] * 2
[ (8) / 2 ] * 2
[ 4 ] * 2
= 8

Now, wasn't that easy? You just have to know what you're doing if you want to do it effectively. Heck, if you get smart enough with equations in PHP, you might just be able to make it do your math homework for you =) Now would that be nice?


Edited by KansukeKojima (see edit history)

Share this post


Link to post
Share on other sites

Code <?php
$num[1]= 5;
$num[2] = 3;
$total = $num[1] - $num[2];
echo "The number is $total";
?>
Outcome
The number is 3


Shoudn't it be "The number is 2"? Obviously this guy is good at programming, but kinda weird at math...

Basically doing math in PHP is like doing math in C. Except for the fact that you don't have any of the "math" libraries to work with.

Share this post


Link to post
Share on other sites

Wow this is quite a project you've got here. If I were actually going to write out the code to do this it would be very long, but I can go over a basic overview with a few examples of how I would make something like this...

Alright, so first of all you want php to remember functions and variables. So let's go with your example of parameter. The easiest way to write a function syntax is P(a)=4a
So let's have your php program recognize that syntax. I would highly advise you to use a preg_match for this unless you want to be parsing strings all day. So just an example of how your php code could recognize a function:

$input=$_GET['input'];if(preg_match('/[A-Z]\([a-z]\)=/',$input)){//$input is a function!}else{//$input is not a function, or may be formatted incorrectly}

Alright, so you wanted your users to set many different functions. We'll put this code in a loop in a bit but to prepare for that let's make each function have its own variable so we can store the data from the function. I will do this with a preg_split. The variable will also start with "func_" so that we don't confuse our function variables from regular variables we are using.

$input=$_GET['input'];if(preg_match('/[A-Z]\([a-z]\)=/',$input)){//$input is a function!$split=preg_split('/\([a-z]\)=/',$input);${"func_".strtolower($split[0])}=$split[1];}

And let's not forget about that parameter, it will be important later so go ahead and start a new group of variables starting with "param_" to store these in.

$input=$_GET['input'];if(preg_match('/[A-Z]\([a-z]\)=/',$input)){//$input is a function!$split=preg_split('/\([a-z]\)=/',$input);${"func_".strtolower($split[0])}=$split[1];${"param_".strtolower($split[0])}=$input[2];echo $param_f;}

Now you would do the same thing for variables just take out the parameter part. Alright so now that you've got function and variable definition set, lets put them to use. There would be two parts to this, one would be getting the answer to a function when calling it with a number as a parameter, and the other would be calling it with a variable as a parameter.
Now lets make a check to see if somebody called the function with a number:
if(preg_match('/[A-Z]\([0-9]{?}\)=/',$input)){

and if so evaluate the function with all instances of that variable swapped out.
WARNING: DO NOT FORGET TO PARSE OUT THE SEMICOLON OR YOUR USERS COULD DO MAJOR DAMAGE TO YOUR WEBSITE!
f(preg_match('/[A-Z]\([0-9]{?}\)=/',$input)){return eval(str_replace(array(";",${"param_".strtolower($input[0])}),array("",substr($input,2,strpos(")")-2))),${"func_".strtolower($input[0])}));}

and for evaluating the function if somebody calls it with a variable would be the same except instead of substr($input,2,strpos(")")-2) put in the variable name of the variable they inserted. So probably something like this:

${"vars_".strtolower($input[2])}

Alright, so now what I would do is put this all into a function and make all of the variables and functions global. Then for every line of math that this php page takes in, run the function for it.

So now your page should successfully be able to do this:

INPUT:F(x)=cos(x)
P(a)=4*a
P(4)
b=17
P(B)
d=6.28318
F(d)

OUTPUT:
null
null
16
null
68
null
1



Again, none of this code was tested by me yet so if you have any errors just post em up here.

P.S. I would suggest writing my own evaluation function rather that using eval, for security reasons and so if people enter variables into the function that aren't passed in the parameters they don't get an error.
Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

Like the one, where you find the future value, let' s say of your investment:FV=PV(1+I)^N, where:FV-->Future ValuePV-->Present ValueI-->Interest Rate^-->powered, N times in our caseN-->number of years.

Share this post


Link to post
Share on other sites

Well yes, if you used this code then you could make that into a function, but you would need to modify the code I gave you to take multiple paramaters for functions like that, not just single ones.

Share this post


Link to post
Share on other sites

Code by galexcd is by design insecure, eval'ing user input is disaster, even if you replace the semicolons. It is possible to execute virtually all php code on one line with no semicolons:<?phpDestroy_website() . Print(get_db_pass()) . Rewrite_php_files_on_server() . Completely_hack_site()?>All possible without semicolons. :P

Share this post


Link to post
Share on other sites
Replying to alex1985Alex - I am new to PHP and looking for the same answer you were previously regarding mathmatical scripting. I require some help in the matter below, can your or anyone help...Be aware I am new to this so will not understand all the tech stuff (yet)!!I am trying to input a few simple mathematical equations without the use of a database. Using the GUI as the input/output interface, I am trying to show ten empty boxes (on my web page, one below the other) from which I wish to be able to input any numbers from 1 -10 in each box and then get an output in a totals box at the bottom of the web page. If this cannot be done please let me know as I will have probably wasted hours looking for this solution.-reply by Steve J

Share this post


Link to post
Share on other sites

I am not actually the best one, just learning the things step by step.

I want to congratulate you, soon you will be the best at php :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.