+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: PHP: New Calculator script

  1. #1
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    PHP: New Calculator script

    G'day everyone. As you can see I have posted my Very Basic Calculator script, I didn't like the code since I only had a short time to make it. Today I have been working on this and I have fixed up a fair bit of the code.

    I will write a tutorial for this one, but I would just like to see what everyone else thinks of this script. Most of the things you will see there isn't much of a difference to the layout but under the hood there is heaps.

    I would like the thank everyone for commenting on my old script and a BIG thanks to Chili. He's script was the one that I got some ideas from and turned that old script into something better.

    It now has an error shown when you try to divide by zero. It has support for any base and exponent when using power to. It uses functions instead of repeating if statements, I tried adding a 1000 seperater using the number_format() function but I couldn't get it to work. Also tried to add an error message when you don't select a number which I will work on the for the next version.

    It has 3 include files but works on the one page for faster processing. It has a better code layout then before.

    I hope use like it and enjoy my new script
    NOTE: Link to Chili's Calculator.
    Yet again thanks to him and everyone who helped me with my old script


    Here are the files.
    functions.php
    Code:
    <?php
    function checkEmpty() {
        global 
    $option;
        if (empty(
    $option)) {
            echo 
    "You must choose what function you are going to be using.";
        } else {
            
    $option $_POST['opt'];
        }
    }
    function 
    addNum() {
        global 
    $num1;
        global 
    $num2;
        
    $ans $num1 $num2;
        echo 
    "$num1 + $num2 <br />";
        echo 
    "= $ans";
    }
    function 
    subNum() {
        global 
    $num1;
        global 
    $num2;
        
    $ans $num1 $num2;
        
    $ans2 $num2 $num1;
        echo 
    "$num1 - $num2 <br />";
        echo 
    "= $ans <br /><br />";
        echo 
    "$num2 - $num1 <br />";
        echo 
    "= $ans2";
    }
    function 
    mulNum() {
        global 
    $num1;
        global 
    $num2;
        
    $ans $num1 $num2;
        echo 
    "$num1 * $num2 <br />";
        echo 
    "= $ans";
    }
    function 
    divNum() {
        global 
    $num1;
        global 
    $num2;
        if (
    $num2 == 0) {
            echo 
    "You can't divide by zero.";
        } else {
            
    $ans $num1 $num2;
            
    $ans2 $num2 $num1;
            echo 
    "$num1 / $num2 <br />";
            echo 
    "= $ans <br /><br/>";
            echo 
    "$num2 / $num1 <br />";
            echo 
    "= $ans2";
        }
    }
    function 
    powNum() {
        global 
    $num1;
        global 
    $num2;
        
    $ans = (pow($num1$num2));
        echo 
    "$num1<sup>$num2</sup> <br />";
        echo 
    "= $ans";
    }
    ?>
    variables.php
    Code:
    <?php
    $option 
    $_POST['opt'];
    $num1 $_POST['num1'];
    $num2 $_POST['num2'];
    $ansformat number_format($ans0'.'',');
    $ans2format number_format($ans20'.'',');
    ?>
    index.php
    Code:
    <?php
    require "functions.php";
    require 
    "form.php";
    require 
    "variables.php";

    if(!isset(
    $_POST['submit'])) {
        
    formInput();
    } else {
        switch (
    $option) {
            case 
    "+":
                
    addNum();
                break;
            
            case 
    "-":
                
    subNum();
                break;
            
            case 
    "*":
                
    mulNum();
                break;
                
            case 
    "/":
                
    divNum();
                break;
            case 
    "^":
                
    powNum();
                break;
        }
        echo 
    "<br /><a href='index.php'>Another calculation</a>";
    }
    ?>
    form.php
    Code:
    <?php
    //This is the form code which will be shown on index.php
    function formInput() {
    ?>
        <center>
            <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <p>
                First/Base number: <input type="text" name="num1" /> <br />
                    Second/Exponent number: <input type="text" name="num2" /><br />
        
                    <input type="radio" name="opt" value="+"/> Addition <br />
                    <input type="radio" name="opt" value="-"/> Subtract <br />
                    <input type="radio" name="opt" value="*"/> Multiply <br />
                    <input type="radio" name="opt" value="/"/> Divide <br />
                    <input type="radio" name="opt" value="^"/> Power
                </p>
                    <input type="submit" value="Submit" name="submit" />
            </form>
        </center>
    <?php
    }
    ?>
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: PHP: New Calculator script

    To display a message when a number is left empty use:

    Code:
    if (trim($_POST['num1']) == "") {
    echo 
    "Number 1 cannot be empty. Defaulting to 1.";
    $num1 1;

    Also you know why the number_format thing ain't working now. That's a great idea.

    Nice job

  4. #3
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: PHP: New Calculator script

    Thanks for the tip mate, I will add more things like that tomorrow after school. hehe. I took things from you and you take things from me. This is an example of sharing
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: PHP: New Calculator script

    Good work, Brandon! The only thing I don't like is the <center> tags, but... I'm a freak, you can ignore me.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: PHP: New Calculator script

    Ooh terrible center tags. lol

    Excellent work on this calculator. Can't wait to see more of it.

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: PHP: New Calculator script

    Yes, CSS would be much better.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    irving is offline Newbie
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Re: PHP: New Calculator script

    i was hoping that someone would teach me how to create calculator using visual basic programming language version 6.0

  9. #8
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: PHP: New Calculator script

    I would look around on google for one. This topic is about a PHP calculator, and actually making calculators are really easy. After you learn the language, it is simple.

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: PHP: New Calculator script

    Chili is right; I can't think of any language that doesn't have mathematical operators.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  11. #10
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: PHP: New Calculator script

    It would be a useless language if it didn't. lol

    Why don't you add square root? Theirs a sqrt() function you can use.

+ Reply to Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. PHP calculator script
    By chili5 in forum Classes and Code Snippets
    Replies: 56
    Last Post: 08-30-2010, 02:46 PM
  3. GPA Calculator
    By tedmp0816 in forum C and C++
    Replies: 3
    Last Post: 09-13-2009, 06:24 PM
  4. PHP script writing a PHP script
    By ezcat in forum PHP Development
    Replies: 6
    Last Post: 03-08-2009, 04:47 AM
  5. What is the difference between VB script and Java script?
    By newage123 in forum JavaScript and CSS
    Replies: 8
    Last Post: 11-14-2008, 10:49 AM

Tags for this Thread

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