Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: where should functions be placed?

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default where should functions be placed?

    After working with javascript a little bit now I feel ready to try my hand again at php functions. It looks like with javascript functions are stored in the head of the page. With php where should the defined function be placed?
    Last edited by james438; 06-11-2011 at 03:31 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Most would likely say in a separate folder and separate file. Then include the file where needed.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Maybe so, but when a page loads it won't matter where the script is located so long as it is loaded. Should a php function be appropriately located in the head, body, footer, title, before the function is called or does it matter?
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I always load them towards the top of the code. With php there is no loading in the head or body or where ever cause that has no bearing in php. Php loads from top to bottom of the page and that's really all you need to concern yourself with. With functions they can be called from anywhere in the page but you must have included them beforehand to be able to use them.

    I am currently building a CMS and within the first few lines I have a foreach that includes all the needed function files before anything else cause directly after that those functions are used to get settings and all sorts of other things.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Short answer: define your functions separately, before any of the content.

    Personally I prefer to create a function library in separate files, and if the site is big enough I have several function definition pages (including classes, perhaps system settings and other things too) all loaded through a common base configuration page. For smaller projects or one-page scripts, I include all functions at the top then do all of the content and other things after that. Either way, functions are separated from the rest.

    To some degree, there's no technical reason that this organization is required, but I strongly prefer it so I can keep everything organized in my mind. If you aren't being very careful with a script, it's not that bad to define things in sections, as long as you place functions before they are actually used.

    I was under the impressions that functions MUST be defined before they are called. That's different than Javascript, where Javascript reads everything and does not (at least not only) operate procedurally from the top of the code to the bottom.
    However, the following code actually works just fine:
    PHP Code:
    <?php
    hello
    ();
    function 
    hello() { echo 'hello'; }
    ?>
    I strongly recommend against that though. It's confusing, not helpful, and CAN get you intro trouble if things get complicated.


    I think the basic explanation is this: PHP defines functions when it gets to that line to process them. I know this is the case because you can have functions defined inside conditional (if) statements and they only get defined if that conditional statement is evaluated. But then there is an exception as shown by the example above: apparently they are also pre-processed if they are in global, immediately accessible scope. So I do think that's an exception, not the standard way.

    Here's a demonstration of how that may work:
    PHP Code:
    <?php
    hello
    ();
    if (
    1==1) { function hello() { echo 'hello'; } }
    ?>
    Even though 1==1 always evaluates as true, PHP doesn't parse the IF until it runs-- but it jumps ahead and parses global-scope functions apparently.


    The only downside to defining functions early is that they take up memory. So I guess technically if you have a huge function you will only need to run at the end of every page, defining it early will waste storage (and processing?) if it's parsed early rather than later on the page. But of course at that point you'd need to embed it somehow so it's not in the global scope mentioned above.


    Complications:
    1. Functions can only be defined once and can never be changed. For this reason, doing anything complex is probably not that relevant.
    2. In embedded contexts such as ifs or loops, functions may actually be variably (or not) defined. This gets really messy and it's usually a bad idea-- better to handle the if within the function than to define it two alternative ways, but it is possible.
    3. Very importantly, if you are including pages, includes do NOT execute until that line is reached in parsing. So that function will NOT be available for previous uses. For this reason alone, it makes sense to always define functions first, in case you ever split a page in two using includes. Interestingly, this is one way that includes don't just "merge" two pages seemlessly. In terms of code, they effectively do that, but apparently not in terms of parsing that code originally.


    Hope that answers your question.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Something that is important here is the way you think about php.

    javascript, obviously, is "part of" your webpage. it is sent with (or to) your webpage, and does things "on" your webpage.

    php, however, is different. writing php as "part of" your webpage is limiting - it cuts you off from php's full power, almost always results in very sloppy code, and prevents you from really understanding how it works.

    php happens on your server, before your webpage. not during, not after. php writes html code. ideally, all of your php should be separate from your html, and finish doing everything it will do first, with only one echo at the end: the completed html document.

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Interesting thoughts. I was looking at a function I was given and confused that the function was defined after it was called, which is why I thought to ask about the correct procedure for coding functions, but I didn't think to ask.

    This is enough to get me started on a project or two. Thanks for explaining. I plan to use your posts as a reference. I do wish that defining functions early did now waste resources however, otherwise it might be nice to define them all together early in the php file to make the function easy to locate.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by james438 View Post
    Interesting thoughts. I was looking at a function I was given and confused that the function was defined after it was called, which is why I thought to ask about the correct procedure for coding functions, but I didn't think to ask
    PHP parses everything first, and then executes the code. (that's why you get absolutely nothing when you have a syntax error, even if it's the last line of the script.) Functions, classes, etc. can be defined anywhere. Variables, constants, and so forth are set during execution, so they need to be set before they are used.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Functions, classes, etc. can be defined anywhere.
    As I explained in my post, this is a risky assumption: it does work most of the time, but they also can be parsed at run time, not when initially parsing the code as a whole. This occurs with includes, and when functions are defined in any sort of embedded context-- ifs, loops, etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    yes - sorry for the oversight. an included script isn't parsed (obviously) until it is included.

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
  •