Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re-use chunks of code?

    Hello. In my script I have a few pieces of code that I use more than once, and to save space I'd like to save it as a function and call it when I need it, rather than have the code copied over and over. The problem is, the code is repeated *within* other functions. i.e:
    PHP Code:
    function usedOver() {
         
    $returnCode "this is code";
         }
    function 
    func1() {
         
    usedOver();
         if(
    $returnCode) {
              echo 
    $returnCode;
              }
         }
    function 
    func2() {
         
    usedOver();
         if(
    $returnCode) {
              echo 
    $returnCode;
              }
         } 
    Obviously this is over-simplified, but you get the idea. My problem is that $returnCode is not passed from usedOver(); into the other functions [i.e. if($returnCode) returns false]. Is there a way to fix this, or is there a better way to reuse code? I'm a beginner, so I welcome any suggestions.

    THANKS!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To return the code you need to assign it to a variable. The variable "$returnCode" you create inside the function "usedOver" only exists inside that function. What you need to do is pass the returned code back into another variable, like this:

    PHP Code:
    function usedOver() { 
         
    $returnCode "this is code"
         } 
    function 
    func1() { 
         
    $blah usedOver(); // you can call this variable anything you like
         
    if($blah) { 
              echo 
    $blah
              } 
         }
    function 
    func2() { 
         
    $returnCode usedOver(); // you can use the same variable name though in practice most people don't
         
    if($returnCode) { 
              echo 
    $returnCode
              } 
         } 
    In this example the code returned from usedOver() is now stored inside func1 in a variable called $blah. You could call the variable $blah anything you want, even $returnCode if you wish, since it only exists inside the function and likewise for the variable inside the function usedOver, although generally a different variable name is used to avoid confusion.

    Usually if a variable is created outside a function it isn't available inside a function unless you specifcally pass it to the function. The same is true in reverse, a variable created inside a function isn't available outside the function or in other functions, you have to pass the data in the manner I demonstrated above.

    There are some exceptions to this, for instance if you create a variable outside of a function then declare the variable as "global" inside the function you can access it directly without having to pass it into and out of the function. Search around for some beginner's tutorials on "variable scope" to explain it all in more detail. Hope this helps some, though.
    Last edited by delinear; 06-11-2005 at 01:47 AM.

  • #3
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, delinear (you always save the day!)
    The problem persists, however. Here's the real code, maybe I missed something important while trying to simplify it...

    This is the block used over more than once:
    PHP Code:
    function findEditable() {
    global 
    $searchThis;
    //// HERE IT STARTS
        
    $split preg_split('/(<!-- OPEN .*? CLOSE -->)/i'$searchThis, -1PREG_SPLIT_DELIM_CAPTURE); 
        for(
    $i=0$i count($split); $i++) { 
            if(
    substr($split[$i], 09) == '<!-- OPEN'$subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i'$split[$i], -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE);
            }
        for(
    $i=0$i count($subSplit); $i++) { 
            
    $returnCode[$i]['id'] = $subSplit[$i][0]; 
            
    $returnCode[$i]['type'] = $subSplit[$i][1];    
            
    $returnCode[$i]['code'] = $subSplit[$i][2];      
            }
    //// HERE IT ENDS
    echo '<form action="'.getenv('SCRIPT_NAME').'" method="post" name="form_edit"><table border="1px" width="600px">';
        if (
    $returnCode) { 
    Now, when I try making the code into a function splitComments() and including it like so:
    PHP Code:
    function splitComments() {
        
    $split preg_split('/(<!-- OPEN .*? CLOSE -->)/i'$searchThis, -1PREG_SPLIT_DELIM_CAPTURE); 
        for(
    $i=0$i count($split); $i++) { 
            if(
    substr($split[$i], 09) == '<!-- OPEN'$subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i'$split[$i], -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE);
            }
        for(
    $i=0$i count($subSplit); $i++) { 
            
    $returnCode[$i]['id'] = $subSplit[$i][0]; 
            
    $returnCode[$i]['type'] = $subSplit[$i][1];    
            
    $returnCode[$i]['code'] = $subSplit[$i][2];      
            }
        }
    function 
    findEditable() {
        global 
    $searchThis;
        
    $returnCode splitComments();
        echo 
    '<form action="'.getenv('SCRIPT_NAME').'" method="post" name="form_edit"><table border="1px" width="600px">';
        if (
    $returnCode) { 
    if ($returnCode) still does not return true. Did I screw up somewhere? When it is like the first example, everything works fine. I am just doing this to cut down on the amount of code, because it is used I think three more times.

    ???

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I must be tired! I forgot to mention you have to specifically return the code from the function, so my example should have said:

    PHP Code:
    function usedOver() {  
         
    $returnCode "this is code";
         return 
    $returnCode;
         } 
    The return statement there tells the function to send the variable back to whatever called it and there it can either be stored or echo'd or whatever.

    To return the data from your function, you just need to add a return line like the above:

    PHP Code:
    function splitComments() { 
        
    $split preg_split('/(<!-- OPEN .*? CLOSE -->)/i'$searchThis, -1PREG_SPLIT_DELIM_CAPTURE);  
        for(
    $i=0$i count($split); $i++) {  
            if(
    substr($split[$i], 09) == '<!-- OPEN'$subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i'$split[$i], -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE); 
            } 
        for(
    $i=0$i count($subSplit); $i++) {  
            
    $returnCode[$i]['id'] = $subSplit[$i][0];  
            
    $returnCode[$i]['type'] = $subSplit[$i][1];     
            
    $returnCode[$i]['code'] = $subSplit[$i][2];       
            }
         return 
    $returnCode
        } 

  • #5
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No problem.. that's something I probably should have known in the first place. Anyway, worked like a charm! As always, thank you much for the assistance!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •