SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a function

    Hi guys

    Is it possible to call a function in different file? If yes how?

    cheers

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a function needs to be defined before php can use it. once it is defined, it can be used anywhere in the script.

    look at require_once() which is a variation of include()

    its common practice to have a dedicated file for function definitions.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, say we have this code

    foo.php
    PHP Code:
    <?

    function foo() {
    $x=1$y=2
    echo $x $y;
    }

    //calling function 
    foo();

    ?>
    so how do i call function foo only in a file named foo2.php

    thanks

  4. #4
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is.

    You call it the same as you would any other function. Except at the top you have to do an include or a require of the file that contains the function.

    PHP Code:
    include '/Functions/PHPFunctionCodeThingy.php'
    Then you can call whatever function is in PHPFunctionCodeThingy.php

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    require_once 'your_functions_file.php';

    foo(); 

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    PHP Code:
    require_once 'your_functions_file.php';

    foo(); 
    thanks calmcrusher

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Note that if you have the function call in the included file (just like you do in foo.php), you don't need to call it after you include the file. Look at it as concatenating the two files.
    Saul

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon
    Note that if you have the function call in the included file (just like you do in foo.php), you don't need to call it after you include the file. Look at it as concatenating the two files.
    php_daemon, foo.php was just for illustrative purposes, but what you saying makes sense.

    cheers


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
  •