Including functions, scope.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • methylparabex
    New Member
    • May 2007
    • 3

    Including functions, scope.

    Hi everyone, I'll try to be brief and to the point - i'm starting to incorporate include() into my php pages to clean up the code but seem to run into problems when trying to call functions that are included in different files. I've been trying to do some research on function scope and please excuse my ignorance, but is this at all possible? For example we have two files:

    File 1: function.inc

    <?php function test() {

    switch ( $var ) {
    case ( $var < 130 ):
    echo " A " ;
    break;
    case ( $var < 480 ):
    echo " B ";
    break;
    case ( $var < 1230 ):
    echo " C ";
    break;
    case ( $var < 1830 ):
    echo " D ";
    break;
    case ( $var < 1831 ):
    echo " E ";
    break;
    }
    }
    ?>

    And to clean up another page, in another file, index.php - I'd like to be able to call the function test() ...

    File 2: index.php

    <head>
    <title>Testin g Page</title>

    <?php include 'function.inc'; ?>

    </head>

    <body>

    <?php test(); ?>

    </body>
    </html>

    Could anyone please help me? And if this isn't possible, is there any kind of workaround for these kinds of things? Thanks in advance.

    Nick
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    This is possible and For your test function you have o pass function parameters also.

    for a example. we will get your func.inc file

    [PHP]<?php
    function test($var)
    {

    switch ( $var ) {
    case ( $var < 130 ):
    echo " A " ;
    break;
    case ( $var < 480 ):
    echo " B ";
    break;
    case ( $var < 1230 ):
    echo " C ";
    break;
    case ( $var < 1830 ):
    echo " D ";
    break;
    case ( $var < 1831 ):
    echo " E ";
    break;
    }
    }
    ?>[/PHP]

    then from your calling script use like this.

    [PHP]<?php
    include 'func.inc';
    $var = 500; //change the value here and call for the script
    test($var);
    ?>[/PHP]

    Comment

    • Purple
      Recognized Expert Contributor
      • May 2007
      • 404

      #3
      Hi,

      An alternative (and IMO better) method to achieve what you are looking for would be to put this and any other standard functions within a class - this will provide much more flexibility around how you use, reuse and maintain the functions included within the class.. there are a number of tutorials around the net if you are not familiar with the concept.. You could take a look at http://www.php-editors.com/articles/...hp_classes.php to start you off.

      Rgds Purple

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi, methylparabex, and wecome to TSDN.

        I have edited the threads title to better describe its topic.
        Please read the Posting Guidlines before posting.

        MODERATOR

        Comment

        • methylparabex
          New Member
          • May 2007
          • 3

          #5
          Thank you for everyone's help and I'll let you know how it goes. I'm definitely going to try those php classes. So thank you and I'll be sure to post accordingly next time.

          Nick

          Comment

          • methylparabex
            New Member
            • May 2007
            • 3

            #6
            I've gotten the functions to work with parameters, now is there any way to perform this same idea with remote functions without parameters? Thanks again for everyone's help.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              You can define global variables that can be used in any part of you code.

              This can be done like this:
              [PHP]
              // Create a test function
              // this could be included from another file
              function testFunc()
              {
              echo $GLOBALS['MyVar'];
              }

              // Define a global variable
              $GLOBALS['MyVar'] = "I am a global vairable!";

              // Call the test function
              testFunc();

              // This will output: I am a global variable
              [/PHP]

              Comment

              Working...