SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question calculator coding

    Found this on the net but couldn't get it working - any trouble for anyone else?

    Calculator.php

    PHP Code:
    <?php
    function square$number )
    {
    $newNumber $number $number;
    return 
    $newNumber;
    }
    function 
    cube$number )
    {
    $newNumber $number $number $number;
    return 
    $newNumber;
    }
    ?>
    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-gb">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <link rel="stylesheet" type="text/css" href="calculator.css">
    <title>Calculator</title>
    </head>

    <body>
    <div align="center">
      <center>
    <form method="POST" action="calculator.php">
    <p>To perform calculations such as squares,  square roots,  and 
    cubes you&nbsp; need only fill in the first number field!<br><br>
    </p>
    <table border="0" cellspacing="1" width="304" id="calculator" height="139">
      <tr>
        <td width="292" align="center" height="39">
        <h3>&nbsp;Calulator</h3>
        </td>
      </tr>
      <tr>
        <td width="292" align="center" height="23">
        <input type="text" name="this" size="10"><br>    <input type="text" name="that" size="10"></td>
      </tr>
      <tr>
        <td width="292" align="center" height="24">
        <input type="submit" value="+" name="do">
        <input type="submit" value="-" name="do">
        <input type="submit" value="*" name="do">
        <input type="submit" value="/" name="do"><br>
        <input type="submit" value="^2" name="do">
        <input type="submit" value="^3" name="do">
        <input type="submit" value="SqRt" name="do">&nbsp;
        </td>
      </tr>
    <?php

    if ($do == "+"){
        if ((
    $_POST['this'] != "") && ($_POST['that'] != "")) {
        
    $result $_POST['this'] + $_POST['that'];
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "-"){
        if ((
    $_POST['this'] != "") && ($_POST['that'] != "")) {
        
    $result $_POST['this'] - $_POST['that'];
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "*"){
        if ((
    $_POST['this'] != "") && ($_POST['that'] != "")) {
        
    $result $_POST['this'] * $_POST['that'];
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "/"){
        if ((
    $_POST['this'] != "") && ($_POST['that'] != "")) {
        
    $result $_POST['this'] / $_POST['that'];
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "^2"){
        if (
    $_POST['this'] != "") {
        
    $result square $_POST['this'] );
        
    $that "";
        }
        else {
        
    $message "<font color=\"red\">You did not fill in the first field!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "SqRt"){
        if (
    $_POST['this'] != "") {
        
    $result sqrt($_POST['this']);
        
    $that "";
        }
        else {
        
    $message "<font color=\"red\">You did not fill in the first field!</font>";
        
    $result "N/A";
        }
    }
    elseif (
    $do == "^3"){
        if (
    $_POST['this'] != "") {
        
    $result cube $_POST['this'] );
        
    $that "";
        }
        else {
        
    $message "<font color=\"red\">You did not fill in the first field!</font>";
        
    $result "N/A";
        }
    }
    ?>

      <tr>
        <td width="292" align="center" height="19"><br><b>Result: <?php echo($result); ?><br><?php echo($message); ?></b></td>
      </tr>

    </table>
    </form>
      </center>
    </div>
    </body>

    </html>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $do $_POST['do']; 

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    yes - ideal . . . thanx crmalibu!!

  4. #4
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to let you know, PHP has a function build in for squared and cubed, http://uk3.php.net/pow. Instead of:
    PHP Code:
    $square square($number);
    $cube cube($number); 
    Use:
    PHP Code:
    $square pow($number2);
    $cube pow($number3); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •