newbe question about including a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WindAndWaves

    newbe question about including a function

    Hi Gurus

    I hope I am going to make sense with this question:

    I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a
    little, mix with html, etc...). Now, I would like to use a function in my php code, as otherwise I have to type the same thing over
    and over again. With a function, I mean something along of the lines of:

    function x($string) {
    return strlen($string) ;
    }
    but obviously, the function is a bit more sophisticated.

    My question is where should I put the function
    a. in a separate file - and how should I link it in that case?
    b. at the bottom of the file
    c. at the top of the file
    d. I should do it differently altogether

    I would appreciate some hints. TIA

    - Nicolaas


  • Colin McKinnon

    #2
    Re: newbe question about including a function

    WindAndWaves wrote:
    [color=blue]
    >
    > My question is where should I put the function
    > a. in a separate file - and how should I link it in that case?
    > b. at the bottom of the file
    > c. at the top of the file[/color]

    Any of the above may be appropriate. The important think is to have a plan
    and stick to it. The overhead of 'including' a file is quite low, but does
    present the issue of maintaining a shared file in an environment with no
    compilation / build verification.

    The rules I apply are usually this:

    1) no more than 20 files in any one directory
    2) a directory for each 'task'
    3) only include files from current directory or from out-of-document-root
    library directories
    4) no output in include files (except where explicitly invoked from the
    including script)
    5) include files named with '.inc.php' or '.inc' to differentiate from
    normal php files
    6) global symbols (function, class names, global vars) in 'library' include
    have a consistent prefix unique to that file
    7) functions which might be shared (e.g. between data-input and viewing
    scripts) defined in an include file
    8) php scripts (not include files) structured as follows:
    [ comment describing file (php)]
    [ includes (php)]
    [ declaration of globals / defines (php)]
    [ class definitions (php)]
    [ function definitions (php)]
    [ 'main' (php)]
    [ additional html (html)]

    You might want to take a look at the Pear style guidelines
    (http://pear.php.net/manual/en/standards.php).

    HTH

    C.

    Comment

    • Erwin Moller

      #3
      Re: newbe question about including a function

      WindAndWaves wrote:
      [color=blue]
      > Hi Gurus
      >
      > I hope I am going to make sense with this question:
      >
      > I have an html page that I have turned into a php page with a bit of php
      > code above the html (connect to database, massage data a
      > little, mix with html, etc...). Now, I would like to use a function in my
      > php code, as otherwise I have to type the same thing over and over again.
      > With a function, I mean something along of the lines of:
      >
      > function x($string) {
      > return strlen($string) ;
      > }
      > but obviously, the function is a bit more sophisticated.
      >
      > My question is where should I put the function
      > a. in a separate file - and how should I link it in that case?[/color]

      Yes.
      look at www.php.net
      search for the command include
      or include_once
      or require

      That does excactly what you ask for.
      [color=blue]
      > b. at the bottom of the file[/color]

      Doesn't matter.
      Once a function is on the page, PHP will find it.
      [color=blue]
      > c. at the top of the file[/color]

      see b
      [color=blue]
      > d. I should do it differently altogether[/color]

      No, your plan is ok.
      [color=blue]
      >
      > I would appreciate some hints. TIA
      >
      > - Nicolaas[/color]


      Good luck.

      Regards,
      Erwin Moller

      Comment

      • Chung Leong

        #4
        Re: newbe question about including a function


        "WindAndWav es" <access@ngaru.c om> wrote in message
        news:Ta3Id.1016 6$mo2.762262@ne ws.xtra.co.nz.. .[color=blue]
        > Hi Gurus
        >
        > I hope I am going to make sense with this question:
        >
        > I have an html page that I have turned into a php page with a bit of php[/color]
        code above the html (connect to database, massage data a[color=blue]
        > little, mix with html, etc...). Now, I would like to use a function in my[/color]
        php code, as otherwise I have to type the same thing over[color=blue]
        > and over again. With a function, I mean something along of the lines of:
        >
        > function x($string) {
        > return strlen($string) ;
        > }
        > but obviously, the function is a bit more sophisticated.
        >
        > My question is where should I put the function
        > a. in a separate file - and how should I link it in that case?
        > b. at the bottom of the file
        > c. at the top of the file
        > d. I should do it differently altogether
        >
        > I would appreciate some hints. TIA
        >
        > - Nicolaas
        >[/color]

        I usually define my PHP functions in a separate file. Mostly to avoid
        confusing them with Javascript functions that appear in the HTML.

        The general convention is that include statements appear at the top of a
        page before everything else:

        <?

        require("dingo. php");
        require("donkey .php");

        ?>
        <html>
        <head><? donkey_headers( ); ?>
        ....



        Comment

        • WindAndWaves

          #5
          Re: newbe question about including a function


          "WindAndWav es" <access@ngaru.c om> wrote in message news:Ta3Id.1016 6$mo2.762262@ne ws.xtra.co.nz.. .[color=blue]
          > Hi Gurus
          >
          > I hope I am going to make sense with this question:
          >
          > I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a
          > little, mix with html, etc...). Now, I would like to use a function in my php code, as otherwise I have to type the same thing[/color]
          over[color=blue]
          > and over again. With a function, I mean something along of the lines of:
          >
          > function x($string) {
          > return strlen($string) ;
          > }
          > but obviously, the function is a bit more sophisticated.
          >
          > My question is where should I put the function
          > a. in a separate file - and how should I link it in that case?
          > b. at the bottom of the file
          > c. at the top of the file
          > d. I should do it differently altogether
          >
          > I would appreciate some hints. TIA
          >
          > - Nicolaas
          >
          >[/color]

          Thank you all VERY VERY much. Much appreciated. It is always useful to read all the online documentation. But affirmations like
          yours give me three times the confidence to continue along the long and windy road to becoming a PHP expert (I have just started
          walking....)

          Thank you again.


          Comment

          Working...