How to create function in one file and then call it from different files?

Discussion in 'PHP' started by globalcashsite, May 25, 2009.

  1. #1
    Guys

    I have one set of commands that calculates Balance figure. I need balance figure on different pages. Right now I have added that set in each page. But I face difficulty whenever I have to make changes in commands.

    So I want that I create function in one file and then I can call that function from any file that can calculate and bring balance figure for me.

    How to do this, please help me.

    GCS
     
    globalcashsite, May 25, 2009 IP
  2. Chikey.ru

    Chikey.ru Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ...
    may be you want get the function, that should be using within other files?

    include "fun.php";
    fun('sd');

    i think i wrong you understand..
     
    Chikey.ru, May 25, 2009 IP
  3. sssharlasss

    sssharlasss Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You might want to use "include_once" instead of "include," just to be on the safe side.
     
    sssharlasss, May 25, 2009 IP
  4. pdfmania

    pdfmania Peon

    Messages:
    213
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    1. create a PHP file e.g. func.calculate.php (put under "functions" folder for any functions would be fine)

    <?
    function FunctionName($parameterNeeded){
    // make some function here that return the result
    $result = $parameterNeeded; // example
    return $result;
    }
    ?>


    2. in any page that require the FunctionName() function you need to add
    - require_once('func.calculate.php'); or
    - include_once('func.calculate.php');

    to call the function you need only to write this.

    $result = FunctionName($parameterNeeded);

    Sometimes, it's better to put the include_once() or require_once() within the head section so you need not to write it again.

    CMIIW...
     
    pdfmania, May 26, 2009 IP
  5. grabus

    grabus Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so simple you can make all you're functions in one file "functions.php"
    than you include it in the files when you want to call you're functions with
    <?php
    include"functions.php";
    ?>
    PHP:
    then you call it ^^
     
    grabus, May 26, 2009 IP
  6. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you every body, I solved this program with help of you people posts. :D

    GCS
     
    globalcashsite, May 26, 2009 IP