alex1985
Feb 27 2008, 10:59 AM
| | 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. |
Comment/Reply (w/o sign-up)
KansukeKojima
Feb 27 2008, 03:50 PM
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..... QUOTE 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?
Comment/Reply (w/o sign-up)
tricky77puzzle
Feb 27 2008, 10:08 PM
QUOTE(KansukeKojima @ Feb 27 2008, 10:50 AM)  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.
Comment/Reply (w/o sign-up)
alex7h3pr0gr4m3r
Feb 28 2008, 03:53 AM
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: CODE $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. CODE $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. CODE $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: CODE 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! CODE 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: CODE ${"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: QUOTE 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.
Comment/Reply (w/o sign-up)
alex1985
Mar 2 2008, 05:12 PM
What's about formulas in finance?
Comment/Reply (w/o sign-up)
alex7h3pr0gr4m3r
Mar 12 2008, 04:57 PM
QUOTE(alex1985 @ Mar 2 2008, 10:12 AM)  What's about formulas in finance? What formulas are those? Sorry I'm not that familiar with finance.
Comment/Reply (w/o sign-up)
alex1985
Mar 12 2008, 05:51 PM
Like the one, where you find the future value, let' s say of your investment: FV=PV(1+I)^N, where: FV-->Future Value PV-->Present Value I-->Interest Rate ^-->powered, N times in our case N-->number of years.
Comment/Reply (w/o sign-up)
alex7h3pr0gr4m3r
Mar 12 2008, 06:15 PM
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.
Comment/Reply (w/o sign-up)
FeedBacker
May 14 2008, 10:26 PM
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: <?php Destroy_website() . Print(get_db_pass()) . Rewrite_php_files_on_server() . Completely_hack_site() ?> All possible without semicolons. :P
Comment/Reply (w/o sign-up)
FeedBacker
Jun 8 2008, 02:28 PM
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
Comment/Reply (w/o sign-up)
Erdemir
Jun 14 2008, 05:39 PM
QUOTE(alex1985 @ Jun 14 2008, 07:43 PM)  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
Comment/Reply (w/o sign-up)
alex1985
Jun 14 2008, 04:43 PM
I am not actually the best one, just learning the things step by step.
Comment/Reply (w/o sign-up)
Similar Topics
Keywords : php, code, mathematical, applications
- Php Code For Login Form With Validation In Php
(7)
Create Table - Mysql Code - Help
(1) I need your feedback about setting the database issues. Please, review them and correct some entries
in the code if they got some mistakes. This is the code itself: SQL CREATE TABLE `news` (
`id` int(250) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '',
`text` text NOT NULL, `author` varchar(255) NOT NULL default '', `valid` varchar(255)
NOT NULL default '', `date` varchar(255) NOT NULL default '', PRIMARY KEY
(`id`) ) ENGINE = MyISAM ; ....
Php Source Code Unveiled In Browser?
is that possible? (7) I am quite new to PHP and this concern came to my mind after playing around a bit with it... When
PHP is not correctly configured on the web server the source code of a php file we try to access
through a browser will be shown instead of the result of the code itself. This will normally not
happen when PHP is working properly, but I was just wondering if it could still be possible to see
that code if a user wanted to or if something on the server failed. This would for example expose
sensitive information like mysql passwords and so on... Is anything like that possib....
Malicious Code Injection
(3) Hi everyone! This is my first post, so be kind! Basically, I'm trying to get a free host
together so am writing some posts. Here's a little summin' summin' about malicious code
injection with PHP applications. Basically, this security exploit is one of the oldest tricks in
the books and all comes down to the fact that PHP allows execution of both local and remote scripts
with the SAME function... dur. Anyway, this is how it works. Image you've just employed a young
go getter, straight outta uni, who has found becoming a Jack of all trades a sinch. Y....
Php And Mysql Programming
anyone knows a code for mysql and php (2) hi everyone! I am making a program using php and mysql...I am a noob on this so i need your help
guys...I want to make a simple program that will some values and then store them on a database and
then retrieve them...uhmm let me give an example out put of what i need. This is the example say..:
Enter First Name: Enter Last Name:
Enter Age: Enter Address: ..those are the
data needed for input values...my question now is how can I make a database whi....
Php Code Needed Iii
(10) Hello, everyone. I need your help again! Who might create the PHP code, the picture is above
this text. Basically, I want when the user fill in all the information in this form, it
automatically was sent to my email. And, then, the dialog box appears or on the same window, it was
said that your request has been sent. Moreover, if the user did not fill the entire information,
the dialog box appears stating that you did not fill some field. Thanks, for help. You always do
that.....
Php Code Needed
Working Together? (5) Hello, everyone. I need your help again. This forum is quite good for it. Well, I need create a
registration form for my web-site using PHP and SQL. The information it should contain: 1) User
Name 2) First Name 3) Last Name 4) Password 5) e-mail Address 6) Security Image: that images helps
to protect a random registration, for instance, 56+2=where user have to type an answer in order to
finish registration. That's all for today. Anymore things, I will post another post over here.
....
Php Code
Needed?! (15) Well, I am a novice in PHP programming, so there is a script which I wanna get: 1. You go the
web-site 2. On the main screen, there is a some kind of field windows, the one you get used to type
in, when you go to google, for instance. 3. He or she types her email address and it's going to
be saved in my SQL database. 4. That's it. Help me if you can.....
Use Rss In Php Code
(3) so, how can I make RSS reader on my website? thanks in advance....
Will This Code Work
php linking script ?p= (5) hi i'm not that great at php so i'm not to sure if this will work or not. but what i want to
do is be able to use ?p=staff or what ever page name, with out the php extion, and i would like to
no if this simple script i made would work. the code is: CODE $p = $_GET ; if ( !empty($p) &&
file_exists('./' . $p . '.php') && stristr( $p, '.' ) == False ) { // pages
= directory where you store your pages $file = './' . $p . '.php'; } else { //
1.php = defult page $file = './index.php'; } include $file; ?> ....
I Need Some Proof Reading For My Code Please! [resolved]
(7) Well... everything is fine except the Content Select section (refer to the in-code headings)...
thats where it says the error is... could anyone find out why it wont work when I click one of my
links? http://2kart.trap17.com/progress.php for an example of what happens...
//----------------- //portfolio paths //----------------- $portfolio = "/portfolio"; $lay =
"/images"; //------------------ //navigation //------------------ $link = · Home html; $link
= · Portfolio html; $link = · Programming html; $link = · Graphics html; $link
= ....
Html Code Tester. Online Script
(15) Yes, yes. I have another script that I have written and I am distributing. I am not entirely sure if
this works. I have not tested it yet, but I will later and post back with a demo and fix it up.
Current script: CODE //Save this as something like htmltest.php function CheckForm() {
$html_unsafe=$_POST ; //Gives us our user input $html_safe=str_replace(" //Starts security measures
$html_safe=str_replace("?>"," ",$html_safe); //User input now secure server side //Still security
issues client side echo $html_safe; //echos our statement } //End function //Main script....
Awesome Source Code Viewer Script
(7) Hello! I have just came up with a sweet script to show the source code of any website and it only
requires one file. This is the basis of the script and can be customized with CSS and other things
and can be instituted as a public resource. Well I will provide the code and a step-by-step tutorial
on each of its parts. This code has been tested by me. Enjoy! CODE //This little tag starts
our php script and is easily the most important part of the script. //We will start our base script
here. //You can change some of the styles used here to your desired color. if (....
Whats Wrong>?
please see this piece of code and see whats wrong: (9) CODE require('connection2.php'); $select=mysql_query("SELECT * from `users` WHERE
password='$_GET '"); $co=mysql_num_rows($select); if ($co = 1) { session_start();
$s=session_id(); $_SESSION ="yes"; $username=$_GET ; header("location:../main/index2.php?a=$_GET
&s=$s"); //echo " Proceed to Game "; //echo $s; } Now that is a bit of my script for my
login script to authenticate and stuff. Recently my game went down because there was some error in
this. So i kept on trying and it didnt work. Now i found out, wait first let me tell you th....
How To Make A Random 7 Number Code?
(2) I am making a script in php, and for it I need to know how to make a random 7 digit code. I think it
has something to do with md5, but i am not sure. Thanks! EDIT- Can someone please change the title
to "How to make a random 7 digit code in php?" Thanks!....
Php Education Class (first Code)
(0) Hi I want to educate some PHP codes that i think they will be useful for all of you! My 1st code is
this: CODE class calculator { /** * Variable for holding all the numbers to add
* * @var array */ private $numbers = array(); /** * Variable
holding all the digits after the point * * @var array */ private $afterPoint =
array(); /** * Maximum number of digits after the point * that a number has
* * @var int */ private $afterPointLength = 0; /** * Fi....
Php And Mysql Applications
(3) Ok I have started useing PHP and MySQL recently and I am wondering how I go about createing an
application. I looked for tutorials and guids at w3schools.com but couldnt find what i was looking
for. All I really need is a list of a few steps to take to get a general idea on were to start when
makeing an application. I would appreciate any help you may have to offer.....
My Code Doesnt Resize Large Images, Please Help.
(2) Can someone please have a look at the following code, this uploads an image, and make it in 2 sizes,
one size is max. 600 x 800, uploads to images folder and second 120 x 120 and uploads to thumbs
folder. this script works fine, with normal size images, but if i try to upload large pics( for
example, an image with dimension 2432 x 3300, it shows blank page, and uploads the original image
without sizing to "image" folder, and doesnt make any small thumbnail... I hope u understand..
Please someone help me, i shall be so thankful. session_start(); header("Cache-contro....
Some Basic Php Code Snippets For All Levels Of Experience
(3) Most of the code snippets are usually used for community driven sites but they do give some general
idea on how php works. Don't forget if your starting out php for the first time that when
saving php files that you need to have the .php extension on your files or they will not work.
Display Browser info This piece of code displays a user's browser info on how they are seeing
the website CODE Actual Display- QUOTE Firefox - Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Internet Explorer ....
Use Bb Code On Your Site!
Just like on forums! (7) To use this you must have PHP support on your server. Just use this code: CODE $content =
"Hello, World!"; $html = array(' ', ' ', ' ', ' ', ' ',
' '); $replacements = array (' ', ' ', ' ', ' ', '
', ' '); $content = str_replace($html, $replacements, $content); ?> This code can
be very useful. It can be used for word filters to block the use of bad words on your site or for
emoticons.....
Display The Current Date/time
With a simple PHP code (4) Use this code to display the current date and time. CODE $date = date('l dS \of F Y
h:i:s A'); echo "$date"; ?> "l" would display the current day of the week such as
Sunday. d displays the day of the month... such as 1 and S adds the appropriate suffix(st). /of
simply displays the word "of". F displays the current month with no abbreviations while Y displays
the four digit year(2007). "h" displays the current hour with leading zeros if necessary(Ex. 06 for
6 o'clock). "i" displays the minute of the hour with leading zeros if necessary. ....
More Dynamic ?id=browsing With Php (associative Array)
Just create array and watch php code do the rest (1) The thing that has been bugging me for a while was that switch statement that we use to create ID
browsing (some use If-Ifelse but results are the same for both). I wanted to figure out a way to use
more dynamic switch statement so that i only need to update my links array in order to create links
for template. With use of foreach, array_keys, and in_array functions finally i managed to do so.
Also i'm planing on changing foreach with array_walk but i'll do that later. Now for the
code.. First we create an associative array something like this CODE $glavni_....
Wap Source Code Viewer
Mobile/wap source code viewer page (4) This is a source code viewer that will workl on wap/mobile sites but you can easily convert it to
work on web im sure ;-) CODE header("Content-Type: text/vnd.wap.wml"); echo '
'; print " "; if ($url == "") { echo " Enter url: »View source code "; }
if ($url == "$url") { $udata=@file_get_contents("$url"); $udata = str_replace("$","$$",$udata);
$udata = str_replace("&","&",$udata); $udata = str_replace("'","'",$udata); $udata =
str_replace(" $udata = str_replace(">",">",$udata); $udata = str_replace("\"....
Requesting Auto Generating Id Tag In Php Code
Php Coding (3) Hello...I'm designing a website in PHP where ppl can submit their links for "cool sites".
Anyway, when somebody submit's a link to a website for example "http://www.google.com" it
creates an id such as "index.php?id=1134411593". I dont want the links to be converted into
id's. I want it to remain as "http://www.google.com". I have the following coding on
( echo " ). I'm a novice. Please Help!!!! Thanks... Plus I
also want to add the date on when the link was submitted. Please follow our forum rule by making....
Dynamic Image / Signature Generator
a simple code to change text on an image (12) In search of dynamically changing quote, saying or all other types of text on an image I came across
a code that I have modified to fit my initial usage. This procedure requires two files and short
knowledge of PHP. If you are familiar with Trap17's sig rotation code you will understand this
procedure very fast. Code 1: dynamic_sig.php (you can rename this to index.php and you'll see
at the end why) Code 2: a simple text file named anything (I will call it name.txt ) Code 1
CODE header("Content-type: image/png"); $image = imagecreatefrompng("../i....
Adapting Html Code Embed To Work On Phpnuke
Help With This Html Code Pls (7) QUOTE how can get this html code to work on my phpnuke site? what tags would i
have to enable in the $Allowable HTML part of my config.php file?? Edited topic title. Moved to
Programming. ....
Change Permission With Php Code
code to change files' and folders' permissions? (3) As everyone know, there two ways (that I can think of) to change files' and directories'
permissions. One is to change it in your cPanel's Disk Manager and the other is with an FTP
client that supports chmod. Well, I'm doing something for my site that requires files to have
full permissions (Execute, Write, and Read on all three groups). At first, I thought that if I made
the directory 777, then every file created in that directory will be 777 as well. I'm wrong. An
alternative to doing this is to change each file permission myself, but that would be....
Get Filename Of Referring Url
php code to get filename of referring URL (9) Hey /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> I want to know how
to get the filename of the referring URL. Look at the following example: Page A which has a URL of
http://blah.trap17.com/blah/blah1.php redirects the user to Page B which has a URL of
http://blah.trap17.com/blah/blah2.php . Is there a PHP code that I can put on blah2.php that will
output blah1.php? I tried _SERVER ; (please note the code may not exactly be correct as I do not
remember the code /laugh.gif' border='0' style='vertical-align:middle' alt='laugh.gif' />....
Php Clock
source Code (8) Hi Every one i find this code its very easy simple php clock i think you can use it /blink.gif'
border='0' style='vertical-align:middle' alt='blink.gif' /> CODE // Binary Clock // script
copyright© 2002 Andreas Tscharnuter // questions? contact: psychodad@psychodad.at ||
http://www.psychodad.at/clock/ // free to use, copy and modify but leave comments untouched;) //
just include this file where your binary clock should appear // version 1.2 03 September 2003 //
below you can change different settings // and remember to drink m000re milk! $size = "40"; ....
How do you test your php code
(97) We know that php is a server side scripting language. So we will need a server with the php parser
to parse/test our code. How are you doing that. Do you upload it to a server for testing or did you
instal php and the server (apache) on your computer (localhost)....
Looking for php, code, mathematical, applications
|
Searching Video's for php, code, mathematical, applications
See Also,
|
advertisement
|
|