SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Usage of the same Content

    Hi,

    I have this really long code which is used over multiple files.
    However, throughout these multiple files..the difference between the code used is a mere addition of say and additional link or code.

    How do I make use of functions, include()/require() so that I do not need to place these code individually into the different files?

    Thank You
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or a simplier example would be..

    placing the mysql database connection code in a several file and calling them from other files?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  3. #3
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just slap it into a function, so say:

    function functionname($arguments) {
    function stuff, or the stuff in the script, replace the stuff that changes with the $arguments
    print 'stuff';
    echo $arguments;
    }

    Then say you want to add an extra link then:

    $argument = '<a href="link.php">link</a>';

    functionname($argument);

    And then include the function in a an included file.

    OR,

    just include a file that requires $arguments

    Say what arguments is, include/require the file, it uses $arguments, and does its thing.

    As for the connection example, I have a connect.inc file, in it, is the stuff to connect to the database, I just include that on everypage which requires a connection.

  4. #4
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does including uses alot of resources...or .....more time?

    i'm thinking of placing my commonly used functions in a separate file, then include them whereeer its needed.

    btw...if its .inc, can it be viewed from the browser etc.?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yes include uses some resources. But only in that another file has to be opened by the OS. Don't fret over it Good software design and coding for reuse are more important.

    Here's an example of putting code into a function and into an include file
    PHP Code:
    common_functions.inc
    ----------------
    <?php // common_functions.inc

    function foo($inStr) {
       
    // this function appends 'foo' onto the end of the string passed to it
       // and returns the new string.
       
    return $inStr 'foo';
    }
    ?>

    main_script.php
    ---------------
    <?php //main_script.php

    include("common_functions.inc");

    $myString "silly as ";
    $myFooedString foo($myString);

    ?>
    The thing to remember about PHP is that variables that are in global scope have to be declared as being so inside the function for their scope to be visable to the function.

    For example, for our function to be able to the variable $bar in the example below we need to declare it as a global within the function
    PHP Code:
    <?php //foobar.php

    function foobar($foo) {
       
    // this function receives a string $foo
       // and appends the string bar onto it and returns the new string
       
    global $bar;
       return 
    $foo $bar;
    }

    $bar "bar";
    $myFoo "foo";

    echo 
    foobar($myFoo);

    ?>
    But, as you can see from the above example, making the function dependent on some variable that exists outside its scope diminishes the independence of the function and you ability to use re-use it in other scripts. It is desirable to try and make your function as independent as possible from having to know about anything outside its immediate scope. This is a rule of thumb to be broken (I know I break it ). Here is an example of rewritingthe function foobar() to make it more independent and more reusable:
    [php]

    PHP Code:
    <?php //foobar.php

    function foobar($foo$bar) {
       
    // this function receives a string $foo
       // and appends the string bar onto it and returns the new string
       
    return $foo $bar;
    }

    $myBar "bar";
    $myFoo "foo";

    echo 
    foobar($myFoo$myBar);
    ?>


    Also, to answer the question about .inc being visible. You should be able to set these type of permissions in the .htaccess file for the directory. So you can put all your includes in a special directory and put an .htaccess file in there. Don't know off the top of my head what goes in the file to achieve this, but others will. If you have root control over your server, you can always move important include files (such as the one that opens the database connection) to be outside the htdocs path for added security. But that is overkill for a simple include file
    Last edited by freakysid; Jun 15, 2001 at 02:42.

  6. #6
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks freakysid.
    but i have multiple variables to be placed int eh 'global'
    so what do i do?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  7. #7
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems that .inc is read. I actually went back, and my connection file was actually .php, so nothing shows up, but I went to a friend's site, who I knew had named it .inc, because I had just shown that to him for an example, and I was presented with his password and other juicy information, luckily for him none of it will pass through to my long term memory, and it will be all gone by the morning.

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if you have globals in a special include file that always gets included in the scripts for that web site then its not a probem to rely on them. The risk is that you create certain variables in one script and declare them as global in your function but then forget to provide those variables in another script where the same function is being used. So, yeah keeping all truely global variables in a globals inc file is ok. Its the sort of thing that would make an object-oriented purist cringe, but hey this is script-hacking


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
  •