0

Well, I have no idea of this and how to do this but its making me go crazy. so here is the thing..

suppose i have this piece of code in say 20 files.

while(//some_condition):
    .....
   // do something here
    .....
endwhile;

Now i need to wrap this loop within a conditional tag say something like this.

if(// condition_for_if ):
    while(//some_condition):
        .....
        //do something here
        .....
    endwhile;
endif;

The problem is, its a super tedious job if you have this in large number of files, sometimes with multiple instances within one single file.

How can i automate the process? can i write a script in php to do that? At first the idea of installer scripts like the one wordpress has came to my mind but i clearly understood that it cannot be done that way.

What to do?

2
  • 4
    You should have kept your code DRY :) Commented Sep 21, 2012 at 9:45
  • 1
    You'd generally include() the file containing the code which is duplicated. Commented Sep 21, 2012 at 9:47

1 Answer 1

1

Put your code into a function in it's own separate file, include it in the scripts where you use it, and simply call the function where needed, passing along any required arguments. Or you could even make the separate file a class and do it that way.

Sign up to request clarification or add additional context in comments.

4 Comments

okkay! but then i have to call the functions in each of the file right?..i do not want to to that...is there a way where i can just search the project directory and include the if condition before the loops?..regular expressions and CLI is coming in my mind..but i think that will make the process too complicated
I'm not sure about searching the project directory but yes, anytime you need to execute that piece of code you would call that function. Why would you be opposed to that?
say i have created the function and then i have added the necessary code there..i must open each file where i expect my function to run and call the function right? if i have to manually open all the files and call the function, would not i just copy paste the necessary code there instead? that ll be the same amount of labour...and see. the code block has while and endwhile. but the inside code can be different for different files..how am i gonna handle that?
All you have to do is use an include statement at the top of the script where you want to call the function. Using an include allows you to have access to all of the functions within that file and thus the code in that file is re-useable, such as: include 'path/to/file.php'; This is a very basic PHP practice and it's one of the ways to avoid repeating yourself when creating an application. Yes, you have to put an include statement in each script where you need to use the function but that's a lot less code that repeating the entire code block each time you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.