...

View Full Version : Re-use chunks of code?



mtd
06-10-2005, 11:14 PM
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:

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!

delinear
06-11-2005, 01:42 AM
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:


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.

mtd
06-11-2005, 02:06 AM
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:
function findEditable() {
global $searchThis;
//// HERE IT STARTS
$split = preg_split('/(<!-- OPEN .*? CLOSE -->)/i', $searchThis, -1, PREG_SPLIT_DELIM_CAPTURE);
for($i=0; $i < count($split); $i++) {
if(substr($split[$i], 0, 9) == '<!-- OPEN') $subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i', $split[$i], -1, PREG_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:
function splitComments() {
$split = preg_split('/(<!-- OPEN .*? CLOSE -->)/i', $searchThis, -1, PREG_SPLIT_DELIM_CAPTURE);
for($i=0; $i < count($split); $i++) {
if(substr($split[$i], 0, 9) == '<!-- OPEN') $subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i', $split[$i], -1, PREG_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.

???

delinear
06-11-2005, 02:15 AM
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:


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:


function splitComments() {
$split = preg_split('/(<!-- OPEN .*? CLOSE -->)/i', $searchThis, -1, PREG_SPLIT_DELIM_CAPTURE);
for($i=0; $i < count($split); $i++) {
if(substr($split[$i], 0, 9) == '<!-- OPEN') $subSplit[] = preg_split('/<!-- OPEN id=\"(.*?)\" type=\"(.*?)\" -->(.*?)<!-- CLOSE -->/i', $split[$i], -1, PREG_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;
}

mtd
06-11-2005, 04:02 AM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum