#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    65
    Rep Power
    9

    Reusing the same blocks of code in different places


    Possibly a very daft question, but how do you go about using the same block of code in multiple different places in the same page? The sort of thing I am referring to would be similar to require "file.php"; but rather than referencing a different file, I want to reference a block of code in the same file.

    In sudo code, the sort of thing I want to do is:

    PHP Code:

    big 
    else if statement;

    Function 
    1{
    some code...
    big else if statement;
    some code...
    }

    Function 
    2{
    some code...
    big else if statement;
    some code...
    }

    Function 
    3{
    some code...
    big else if statement;
    some code...

    What's the best way to do this?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Originally Posted by dwair
    but rather than referencing a different file, I want to reference a block of code in the same file.
    It's called "function".
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Functions

    If you start getting a lot of them or otherwise just want to take your concepts to the next level you can begin looking into object-oriented programming, though to be used "properly" will be a slight twist from what you're probably used to.
    LinkedIn: Dave Mittner
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    65
    Rep Power
    9
    Thanks for the replys
    Functions within functions within functions within functions within functions - or OOP...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,990
    Rep Power
    375
    Functions within functions within functions within functions within functions

    this is COMPLETELY wrong and i see no real example where you would do this, and not even sure WHY you would do it!

    what he meant was

    function valid($x){
    if ( $x == "" ) {
    return false;
    }
    return true;
    }
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by dwair
    Functions within functions within functions within functions within functions - or OOP...
    Function calls, not functions.

    Functions are always defined on the top level (not within another function!). And then you can call them wherever you want.

    PHP Code:
    <?php

    function walk_the_dog() {
        echo 
    "I'm walking the dog!";
        
    go_back_home();
    }

    function 
    visit_a_friend() {
        echo 
    "I'm visting a friend!";
        
    go_back_home();
    }

    // that's your shared function
    function go_back_home() {
        echo 
    "I'm going back home!";
    }
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo