SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could anyone explain in layman terms how I can use the PHP function command.

    I have got a script which I want to use several times for different scenarios. For each one of these only 1 variable in the script needs to change - therefore creating a function out of the script makes sense.

    I've got a little way to understanding it, but can anyone help me along, and correct me where I'm wrong.

    To create a function I do this:

    <?php
    function my_reusable_function($my_one_changing_variable)
    {
    //Insert the full script here
    }
    ?>

    To run the script with my changing variable I put:

    <?php
    my_reusable_function(the_variable_that_will_change);
    ?>

    If I want to save my functions in a different directory to the scripts that reference them, how do I get the script to know where the function I want to use is?????

    eg If I store my functions in mysite.com/php/functions

    and my script that references it is in
    mysite.com/scripts/myfirstscript.php

    What do I need to insert within the script to find the function?????

  2. #2
    SitePoint Member
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use the php-funtion 'include'. To use it in your script put

    <php include "../php/functions/name_of_file_with_function_to_use_in_it.php" ?>

    somewhere before you call the funtion.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers,

    Is this the same for both PHP4 and PHP3??
    I read somewhere that PHP4 does something along these lines automatically (although I can't see how). I've probably got my wires crossed and its referring to something different.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could set up an auto_prepend file in your php.ini or in a .htaccess file for a particular directory. But that would mean all your functions would be in one file. Or you could just use include() like stated in the last post, which would probably be the preferred method.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I'll see how I get on.

  6. #6
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, if you need your function to take the variable and then return it after its been processed then do this:

    function whatever($test) {
    $test = $test * 5; //or whatever
    return $test
    }

    then do

    $var=whatever($test);

    one questoin: does any one know how to return more than one variable? like return $test, $test2;

    my c++ teacher says you cant (for c++) but i was thinking it would be really silly for them not to have you be able to return more than one variable...

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a case like that I normally do this

    function sample($var1, $var2) {
    $var1++;
    $var2++;
    return $var1."|".$var2;
    }


    $vars = explode("|", sample(2, 4));
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you could return an array (although in PHP, there is no barrier between variable types!)

    But to be simple, Freddy's solution is optimal.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


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
  •