Scientific Calculator in PHP

scientific calculator php
This is a Scientific Calculator written in PHP. It has some of the functions of a standard Scientific Calculator like the Pi (π), Exponents, Square Root, Sine, Cosine, Tangent, Arc Sine, Arc Cosine, Arc Tangent, Logarithm, Natural Logarithm (ln), Factorial (n!), the basic arithmetic operations found in a Standard Calculator – Addition (+), Subtraction (-), Multiplication (x), and Division (÷); Conversion from Degree to Radian and vice versa, and some Memory Storing functions.

It is perhaps one of the longest code I have written so far, though it could have been shorten using functions and classes. Anyway, it took me a couple of days to write everything since I had to study some of the Scientific Calculator’s mathematical equations that I am not familiar of… for instance log. At first, I don’t even know what that is, I thought it is about trees XD. Also I would like to mention mmtuts. Thanks to his tutorial video about Switches, I was able to write my code efficiently using the switch method, instead of using the conditional arguments which I usually use. Now, building the calculator wasn’t easy, it requires some research not only in Math, but also in PHP. Because of it, I was able to get acquainted with some of PHP’s Math functions. The resources for the PHP code for Scientific Calculators also seem to be scarce as most people would prefer to use Javascript over PHP to build a Scientific Calculator.

Here’s the demo:
Scientific Calculator

Brief look into the Source Code

Scientific Calculator PHP Source Code
To achieve the storing value functions, I use the $_SESSION Superglobal Variables. If not for the SESSION variable, the values resulting from the equation and the value entered in the Enter Number Field will disappear without a trace, leaving us with nada. The code is 600+ lines long and we don’t have that much time to explain everything so I’m only going to focus on some of the functions I use. For the session function, please refer to this PHP File as it is explained there how sessions work.

Here are some of the functions you’ll find in the PHP Source Code. (Please refer to the Source Code found below.)

error_reporting(0);

It can be found in Line 2, and from just looking on it, we would immediately get an idea of what it does. If not, well, the function of that code is that it disables PHP error report messages. For instance, when you’re missing a delimiter ;. PHP would normally show a message informing you that you forgot to close your statement. If error_reporting is set to 0, then it won’t show such message. Now, why do we have to do this? Well, that’s because some equations that will be performed by the Scientific Calculator will give out an error message, although it is an intended error. For example, dividing by 0. This will show a PHP warning that tells you that you are dividing by 0. Mathematically speaking, it is not logically possible, but equations like these can’t be avoided. So, to prevent the error message from showing up, we have to disable error reporting.

If Statements

Throughout the code, you’ll find a lot of If Statements. Usually we use this to change the content of the variables. Also, note that the switch function is heavily relying on this as it uses $equate variable as the basis of its functions. $equate value changes depending on what button is pressed in the calculator.

switch ($equate)

Can be found in Line 116. This works like IF statements. Depending on the value of the variable inside the parenthesis (in this case $equate), different functions can be executed. We are using this to easily switch the function of the calculator base on what button is pressed. For example, if $equate = ‘perf_add’ (represented by case ‘perf_add’), in which case, PHP will perform an addition arithmetic.

break;

break; ends the function contained inside the case.

pow

Line 200. pow returns base value raised to the power of the exponent. For this to work, two variable are needed as expressed in ($base,$exp), where base is any base number and exp is any number that is going to be use as the exponential power.

sqrt

Line 250, 256, 262. It means Square root. As it is, it will get the square root of the number. Square root by the way is a number that produces a specified quantity when multiplied by itself.

sin(deg2rad($firstnum));

You’ll find this first at Line 271. sin is a PHP math (Trigonometry) function that gets the sine value of the number. We followed this up with deg2rad that converts our entered number into the radian equivalent, otherwise, our answer will be different from that of other Scientific Calculators. Well, that is the simplest of reasons, but trigonometrically speaking, that is because radians are directly related to pi, and can be thought of as naturally occurring units when dealing with trigonometry.

cos and tan

First occurring at Lines 292 and 313 consecutively. cos is for getting the cosine and tan is for getting the tangent. Go figure.

log

It took me a while to understand this. log stands for Logarithm. It is the inverse function to exponentiation. Meaning, the logarithm of a given number x is the exponent to which another fixed number, the base b, must be raised, to produce that number x (thank you, Wikipedia). However, I didn’t know there is log10 and log, which give out different results. The log function in PHP is equivalent to the ln function while log10 is for the log function in an actual Scientific Calculator. This is why at first I was confuse why PHP is giving out a different result from the one given out by a Scientific Calculator. You can find this code in line 475, 481, and 487. In PHP, log is use to get the natural logarithm (ln) of a number.

log10

log10(x) represents the logarithm of x to the base 10. Mathematically, log10(x) is equivalent to log(10, x). In PHP it does the same thing and is similar to the ln function in a Scientific calculator. First appeared in Line 334.

asin, acos, atan

First found at Lines 355, 379, and 403 consecutively. It stands for arc sine, arc cosine, and arc tangent – the inverse functions of sine, cosine, and tangent. Since they are inverse, we may have to convert their resulting returned values afterwards back to degrees in order to replicate the Scientific calculator results. That’s why we did a rad2deg math function on the result after PHP gave it out.

case ‘perf_fac’:

This is a function worth discussing (see line 423). In this function, we are using a for loop to generate the numbers we are going to use in the equation. Factorial, as we know, is the result of all the numbers in sequence after being multiplied. Sort of like the Summation Notation, where we add all the numbers in succession, only that in this case, we multiply. Factorial is represented as n! in the Scientific Calculator. Example: for a Factorial of 5 (5!), we get the numbers 5 * 4 * 3 * 2 *1 (except 0). n! in this scenario is equivalent to 5, where 5 is the number for our variable. In our PHP file, we store this in variable $firstnum and use it inside the for loop condition:

for ($x=$firstnum; $x>=1; $x--)

We did this so that the loop will do the same function this number of times, decrementing it by 1 after each cycle, thus $x‐‐, and will end once it becomes greater than or equal to 1 as shown $x>=1. This is so that it won’t reach 0 and thus avoid getting 0 in the equation. Each cycle, $factor = $factor * $x, will be performed. PHP will generate $factor which contains the value of 1 and multiply it to $x which starts as the value of $firstnum and will gradually decrement. Thus, for the example 5!, where 5 is the value of $firstnum, we get the equation 1 * 5 * 4 * 3 * 2 * 1, and this will all be multiplied and will become the new equivalent of $factor, which will then be again use by the $_SESSION[‘mem0’] superglobal variable that serves as our answer.

Also note that in line 425, 440, and 454, we have the condition that limits our value to 200. If it exceeds 200, it will return the answer ‘INF’ which means Infinite. Well, this is necessary to prevent PHP from crashing, as a very large factorial like let’s say 1,000,000 would be too much for PHP to handle. Nevertheless, a factorial of 200 and above will result to INF anyway.

deg2rad and rad2deg

We used these on case functions ‘perf_deg’ and ‘perf_rad’ (lines 492 and 506 consecutively) and on the Trigonometric functions. The functions simply convert the value from degrees to radian (deg2rad) and radian to degrees (rad2deg).

The rest of the functions

The rest of the functions are more like playing around with the variables and swapping values to preserve the answer. I need not elaborate on this but just for the sake of an example…

if(!empty($_SESSION['mem0'])) {
	unset($_SESSION['mem1']);
	$_SESSION['mem1'] = $_SESSION['mem0'];
	unset($_SESSION['mem0']);
	$memo1 = $_SESSION['mem1'];
	$ans = 0;
	} else {
				
	}

The code means that if $_SESSION[‘mem0’] is not empty, then we unset the contents of $_SESSION[‘num1], thus its contents will disappear, and then replace it with the content of $_SESSION[‘mem0’]. The value of mem0 has now been transferred to mem1. This is followed by freeing the variable of mem0 and then storing mem1 to another variable $memo1 and setting the content of variable $ans to 0. Else if $_SESSION[‘mem0’] is empty, then do nothing.

Finally, note line 604. default is the final case we have to create. This is the default function in case non of the cases matches the content of $equate. Since we are not going to do anything, we can just leave this empty, and since this is the final case, we need not end it with a break.

If you have questions, feel free to drop me a comment and I will try my best to answer your questions.

PHP File

<?php
	error_reporting(0);
	session_start();
	$time = $_SERVER['REQUEST_TIME'];
	$timeout_session = 1800;
	if(isset($_SESSION['LAST_ACTIVITY']) && ($time - $_SESSION['LAST_ACTIVITY']) > $timeout_session) {
		session_unset();
        session_destroy();
        session_start();
    }
    $_SESSION['LAST_ACTIVITY'] = $time;

    $firstnum = '';
    if(empty($_SESSION['mem1'])){
    	$memo1 = 'Empty';
    } else {
    	$memo1 = $_SESSION['mem1'];
    }
    if(empty($_SESSION['mem2'])){
    	$memo2 = 'Empty';
    } else {
    	$memo2 = $_SESSION['mem2'];
    }
    if(empty($_SESSION['mem3'])){
    	$memo3 = 'Empty';
    } else {
    	$memo3 = $_SESSION['mem3'];
    }
	$ans = 0;
	$equate ='';
	if(isset($_POST['clear'])) {
		$equate = 'perf_clr';
	}
	if(isset($_POST['add'])) {
		$equate = 'perf_add';
	}
	if(isset($_POST['sub'])) {
		$equate = 'perf_sub';
	}
	if(isset($_POST['mul'])) {
		$equate = 'perf_multi';
	}
	if(isset($_POST['div'])) {
		$equate = 'perf_div';
	}
	if(isset($_POST['pi'])) {
		$equate = 'perf_pi';
	}
	if(isset($_POST['expo'])) {
		$equate = 'perf_exp';
	}
	if(isset($_POST['pow'])) {
		$equate = 'perf_pow';
	}
	if(isset($_POST['ex2'])) {
		$equate = 'perf_ex';
	}
	if(isset($_POST['sqr'])) {
		$equate = 'perf_sqr';
	}
	if(isset($_POST['sin'])) {
		$equate = 'perf_sin';
	}
	if(isset($_POST['cos'])) {
		$equate = 'perf_cos';
	}
	if(isset($_POST['tan'])) {
		$equate = 'perf_tan';
	}
	if(isset($_POST['log'])) {
		$equate = 'perf_log';
	}
	if(isset($_POST['rsin'])) {
		$equate = 'perf_sin1';
	}
	if(isset($_POST['rcos'])) {
		$equate = 'perf_cos1';
	}
	if(isset($_POST['rtan'])) {
		$equate = 'perf_tan1';
	}
	if(isset($_POST['fact'])) {
		$equate = 'perf_fac';
	}
	if(isset($_POST['nlog'])) {
		$equate = 'perf_natl';
	}
	if(isset($_POST['deg'])) {
		$equate = 'perf_deg';
	}
	if(isset($_POST['rad'])) {
		$equate = 'perf_rad';
	}
	if(isset($_POST['mem1'])) {
		$equate = 'perf_mem1';
	}
	if(isset($_POST['mem2'])) {
		$equate = 'perf_mem2';
	}
	if(isset($_POST['mem3'])) {
		$equate = 'perf_mem3';
	}
	if(isset($_POST['memclr'])) {
		$equate = 'perf_clrmem';
	}
	if(isset($_POST['m1'])) {
		$equate = 'perf_m1a';
	}
	if(isset($_POST['m2'])) {
		$equate = 'perf_m2a';
	}
	if(isset($_POST['m3'])) {
		$equate = 'perf_m3a';
	}

	switch ($equate) {
		case 'perf_add':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = $firstnum;
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				$_SESSION['num2'] = $_POST['num'];
				unset($_SESSION['mem0']);
				$firstnum = $_SESSION['num2'];
				$_SESSION['mem0'] = $_SESSION['num1'] + $firstnum;
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_sub':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = $firstnum;
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				$_SESSION['num2'] = $_POST['num'];
				unset($_SESSION['mem0']);
				$firstnum = $_SESSION['num2'];
				$_SESSION['mem0'] = $_SESSION['num1'] - $firstnum;
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_multi':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = $firstnum;
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				$_SESSION['num2'] = $_POST['num'];
				unset($_SESSION['mem0']);
				$firstnum = $_SESSION['num2'];
				$_SESSION['mem0'] = $_SESSION['num1'] * $firstnum;
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_div':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = $firstnum;
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				$_SESSION['num2'] = $_POST['num'];
				unset($_SESSION['mem0']);
				$firstnum = $_SESSION['num2'];
				$_SESSION['mem0'] = $_SESSION['num1'] / $firstnum;
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_pi':
			unset($_SESSION['mem0']);
			unset($_SESSION['num1']);
			unset($_SESSION['num2']);
			$_SESSION['mem0'] = 3.14159;
			$ans = $_SESSION['mem0'];
		break;
		case 'perf_exp':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = $firstnum;
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				$_SESSION['num2'] = $_POST['num'];
				unset($_SESSION['mem0']);
				$firstnum = $_SESSION['num2'];
				$_SESSION['mem0'] = pow($_SESSION['num1'],$firstnum);
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_pow':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = pow(10,$firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = pow(10,$firstnum);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = pow(10,$_SESSION['num1']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_ex':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = pow($firstnum,2);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = pow($firstnum,2);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = pow($_SESSION['num1'],2);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_sqr':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = sqrt($firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = sqrt($firstnum);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = sqrt($_SESSION['num1']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_sin':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = sin(deg2rad($firstnum));
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = sin(deg2rad($firstnum));
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = sin(deg2rad($_SESSION['num1']));
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_cos':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = cos(deg2rad($firstnum));
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = cos(deg2rad($firstnum));
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = cos(deg2rad($_SESSION['num1']));
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_tan':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = tan(deg2rad($firstnum));
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = tan(deg2rad($firstnum));
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = tan(deg2rad($_SESSION['num1']));
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_log':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = log10($firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = log10($firstnum);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = log10($_SESSION['num1']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_sin1':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = asin($firstnum);
				$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = asin($firstnum);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = asin($_SESSION['num1']);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_cos1':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = acos($firstnum);
				$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = acos($firstnum);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = acos($_SESSION['num1']);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_tan1':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = atan($firstnum);
				$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = atan($firstnum);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = atan($_SESSION['num1']);
					$_SESSION['mem0'] = rad2deg($_SESSION['mem0']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_fac':
			if(empty($_SESSION['mem0'])) {
				if($_SESSION['num'] > 200){
    				$ans = 'INF';
				} else {
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$factor = 1;
					for ($x=$firstnum; $x>=1; $x--)
						{
							$factor = $factor * $x;
						}
					$_SESSION['mem0'] = $factor;
					$ans = $_SESSION['mem0'];
				}
			} else {
				if($_POST['num'] > 0){
					if($_SESSION['num'] > 200){
    					$ans = 'INF';
					} else {
						$_SESSION['num1'] = $_POST['num'];
						$firstnum = $_SESSION['num1'];
						$factor = 1;
							for ($x=$firstnum; $x>=1; $x--)
							{
								$factor = $factor * $x;
							}
						$_SESSION['mem0'] = $factor;
						$ans = $_SESSION['mem0'];
					}
				} else {
					if($_SESSION['mem0'] > 200){
	    				$ans = 'INF';
					} else {
						unset($_SESSION['num1']);
						$_SESSION['num1'] = $_SESSION['mem0'];
						unset($_SESSION['mem0']);
						$factor = 1;
						for ($x=$_SESSION['num1']; $x>=1; $x--)
							{
								$factor = $factor * $x;
							}
						$_SESSION['mem0'] = $factor;
						$ans = $_SESSION['mem0'];
					}
				}
			}
		break;
		case 'perf_natl':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = log($firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				if($_POST['num'] > 0){
					$_SESSION['num1'] = $_POST['num'];
					$firstnum = $_SESSION['num1'];
					$_SESSION['mem0'] = log($firstnum);
					$ans = $_SESSION['mem0'];
				} else {
					unset($_SESSION['num1']);
					$_SESSION['num1'] = $_SESSION['mem0'];
					unset($_SESSION['mem0']);
					$_SESSION['mem0'] = log($_SESSION['num1']);
					$ans = $_SESSION['mem0'];
				}
			}
		break;
		case 'perf_deg':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = rad2deg($firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				unset($_SESSION['mem0']);
				$_SESSION['mem0'] = rad2deg($_SESSION['num1']);
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_rad':
			if(empty($_SESSION['mem0'])) {
				$_SESSION['num1'] = $_POST['num'];
				$firstnum = $_SESSION['num1'];
				$_SESSION['mem0'] = deg2rad($firstnum);
				$ans = $_SESSION['mem0'];
			} else {
				unset($_SESSION['num1']);
				$_SESSION['num1'] = $_SESSION['mem0'];
				unset($_SESSION['mem0']);
				$_SESSION['mem0'] = deg2rad($_SESSION['num1']);
				$ans = $_SESSION['mem0'];
			}
		break;
		case 'perf_mem1':
			if(!empty($_SESSION['mem0'])) {
				unset($_SESSION['mem1']);
				$_SESSION['mem1'] = $_SESSION['mem0'];
				unset($_SESSION['mem0']);
				$memo1 = $_SESSION['mem1'];
				$ans = 0;
			} else {
				
			}
		break;
		case 'perf_mem2':
			if(!empty($_SESSION['mem0'])) {
				unset($_SESSION['mem2']);
				$_SESSION['mem2'] = $_SESSION['mem0'];
				unset($_SESSION['mem0']);
				$memo2 = $_SESSION['mem2'];
				$ans = 0;
			} else {
				
			}
		break;
		case 'perf_mem3':
			if(!empty($_SESSION['mem0'])) {
				unset($_SESSION['mem3']);
				$_SESSION['mem3'] = $_SESSION['mem0'];
				unset($_SESSION['mem0']);
				$memo3 = $_SESSION['mem3'];
				$ans = 0;
			} else {
				
			}
		break;
		case 'perf_clrmem':
			unset($_SESSION['mem1']);
        	unset($_SESSION['mem2']);
        	unset($_SESSION['mem3']);
        	$memo1 = 'Empty';
        	$memo2 = 'Empty';
        	$memo3 = 'Empty';
        break;
        case 'perf_m1a':
			if(empty($_SESSION['mem1'])) {
				if(empty($_SESSION['mem0'])) {
					$ans = 0;
				} else {
					$ans = $_SESSION['mem0'];
				}
			} else {
				unset($_SESSION['mem0']);
				$_SESSION['mem0'] = $_SESSION['mem1'];
				$ans = $_SESSION['mem0'];	
			}
		break;
		case 'perf_m2a':
			if(empty($_SESSION['mem2'])) {
				if(empty($_SESSION['mem0'])) {
					$ans = 0;
				} else {
					$ans = $_SESSION['mem0'];
				}
			} else {
				unset($_SESSION['mem0']);
				$_SESSION['mem0'] = $_SESSION['mem2'];
				$ans = $_SESSION['mem0'];	
			}
		break;
		case 'perf_m3a':
			if(empty($_SESSION['mem3'])) {
				if(empty($_SESSION['mem0'])) {
					$ans = 0;
				} else {
					$ans = $_SESSION['mem0'];
				}
			} else {
				unset($_SESSION['mem0']);
				$_SESSION['mem0'] = $_SESSION['mem3'];
				$ans = $_SESSION['mem0'];	
			}
		break;
		case 'perf_clr':
			unset($_SESSION['mem0']);
        	$firsnum = '';
    	break;
    	default:
	}
?>
<html>
<head>
	<title>Scientific Calculator</title>
	<meta name="description" content="This is a Scientific Calculator written in PHP." />
    <meta name="robots" content="index,follow" />
    <meta name="keywords" content="scientific calculator, calculator" />
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<center><h1>Scientific Calculator</h1></center>
<p><b>Description:</b> This is a Scientific calculator written in PHP. It has some of the functions of a Scientific calculator. This is just a preliminary version of a more advance calculator that will be written in the future. Written by Altometa.<br/><br/>
<center><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
	Enter Number: <input type="number" name="num" value="0" step="any" required><br/><br/>
	Select an operation:<br/><br/>
	<input type="submit" value="+" name="add">&nbsp;
	<input type="submit" value="-" name="sub">&nbsp;
	<input type="submit" value="x" name="mul">&nbsp;
	<input type="submit" value="÷" name="div">&nbsp;
	<input type="submit" value="Clear" name="clear"><br><br>
	<input type="submit" value="π" name="pi">&nbsp;
	<input type="submit" value="X^" name="expo">&nbsp;
	<input type="submit" value="10^" name="pow">&nbsp;
	<input type="submit" value="X²" name="ex2">&nbsp;
	<input type="submit" value="√" name="sqr"><br><br>
	<input type="submit" value="sin" name="sin">&nbsp;
	<input type="submit" value="cos" name="cos">&nbsp;
	<input type="submit" value="tan" name="tan">&nbsp;
	<input type="submit" value="log" name="log"><br><br>
	<input type="submit" value="sin¯¹" name="rsin">&nbsp;
	<input type="submit" value="cos¯¹" name="rcos">&nbsp;
	<input type="submit" value="tan¯¹" name="rtan"><br><br>
	<input type="submit" value="n!" name="fact">&nbsp;
	<input type="submit" value="ln" name="nlog">&nbsp;
	<input type="submit" value="deg-rad" name="rad">&nbsp;
	<input type="submit" value="rad-deg" name="deg"><br><br>
	<input type="submit" value="M1+" name="mem1">&nbsp;
	<input type="submit" value="M2+" name="mem2">&nbsp;
	<input type="submit" value="M3+" name="mem3">&nbsp;
	<input type="submit" value="MC" name="memclr"><br><br>
	<input type="submit" value="M1^" name="m1">&nbsp;
	<input type="submit" value="M2^" name="m2">&nbsp;
	<input type="submit" value="M3^" name="m3">
</form></center>

<?php
	
//	echo 'Equation: ' . $firstnum . ' ' . $operator . ' ' . $secondnum . '<br><br>';
	echo 'Answer: ' . $ans . '<br><br>';
	echo 'Memory 1: ' . $memo1 . '<br>';
	echo 'Memory 2: ' . $memo2 . '<br>';
	echo 'Memory 3: ' . $memo3;
?>

</body>
Follow me at:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.