Archive for myfreeforum.org Before posting please check the "stickies" in the support forums.
Please ask questions in real English and not "txt". You will get a better response.
Please do not ask support questions via PMs.
 



       myfreeforum.org Forum Index -> Off Topic
CodyT07

Little php help

Been a while since I programmed in php but I heard the idiots that run our school servers might install php, considering they got hacked cause they couldn't install asp   So in hopes of being ahead of the pack in the web design class I been doing a little php reading and testing. So far I did a simple calculator but I been trying to make it a little more advance, one of the problems is doing 'powers'
E.g 3 ^10 which is 3*3*3*3*3*3*3*3*3*3*3*3.
Problem is I have no clue how to program that  , php doesn't know what to do with ^ My guess was a loop but I keep ending with the answer of "0". Anyone know of a direction I should try to do it correct?
If you are noosy here is my calculator so far
Spoiler:

Code:
<?php
//Simple Calculator by CodyT07, nothing special. Just a test script
?>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<p>
To use simply put in 2 numbers and choose your to add, subtract, etc. For conversion you do not have to enter a second number.
<br />
<form name="input" action="calculator.php" method="post">
<br />
First Number <input type="text" name="number1">
<br />
Operator
<br />
<input type="radio" name="operator" value="+"> +
<br>
<input type="radio" name="operator" value="-"> -
<br />
<input type="radio" name="operator" value="*"> *
<br />
<input type="radio" name="operator" value="/"> /
<br />
<input type="radio" name="operator" value="Fahrenheit"> Fahrenheit to Celsius
<br />
<input type="radio" name="operator" value="Celsius">Celsius To Fahrenheit
<br />
Second Number <input type="text" name="number2">
<br />
<input type="submit" value="Submit">
 </form>
  <br />
 
<?php
//Executing Time!

//Sets the numbers submitted to a variable
$num1 = $_POST["number1"];
$oper = $_POST["operator"];
$num2 = $_POST["number2"];

//Echos the numbers inputed
echo "$num1" . "$oper" . "$num2" . "=";  

//Execute the numbers and operator inputed
switch ($oper)
{
//Add
case "+";
$answer = "$num1" + "$num2";
echo "$answer";
break;

//subtract
case "-";
$answer = "$num1" - "$num2";
echo "$answer";
break;

//Multiply
case "*";
$answer = "$num1" * "$num2";
echo "$answer";
break;

//Divide
case "/";
$answer = "$num1" / "$num2";
echo "$answer";
break;

//Fahrenheit to Celsius
case "Fahrenheit";
$answer = ("$num1" - 32) * 5/9;
echo "$answer" . "Celsius";
break;

//Celsius to Fahrenheit
case "Celsius";
$answer = ("$num1" + 32) * 9/5;
echo "$answer";
break;
}




?>
</p>


</body>
</html>


I did set bad numbers for the celsius to Fahr but I will work on that later.
Yes I did all that by myself.
Andrew(AP)

http://huntingsphere.exofire.net/calculator.php

Well i have no idea about the powers but I was bored and did a little remodeling.

Good luck with your calculator though.
CodyT07

After I got the core code down I would of made it look better. Big thing was to fix the "=" showing up for no reason, could add it in the final echo statement.
but that does look good!

Maybe I can make really advance, doubt much more advance but it is a start.
CodyT07

Code:
case "^";
function powers ($num, $power)
{
      // in case garbage is passed in.
   if(!is_numeric($num) || !is_numeric($power))
      return 0;
   if($num<=0 || $power<=0)
      return 0;

   $result = 1;
   for($cnt=1; $cnt<=$power; $cnt++)
   {
      $result = $result * $num;
      
   }
   return $result;
}

$output = powers($num1,$num2);
echo($output);
}

Seems to be it...
Going to try other mathematical equations too.
interlog

use:

pow(base, power)

e.g

echo pow(3,10);

Mark  
CodyT07

interlog wrote:
use:

pow(base, power)

e.g

echo pow(3,10);

Mark  

*slaps self*. Thanks for that.

       myfreeforum.org Forum Index -> Off Topic
Page 1 of 1
Create your own free forum | Buy a domain to use with your forum