Link to home
Create AccountLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

want to reuse code (for read and write) without doing a function

<?php

$action='read';

  if($action=='read'){
    //begin year
    for ($year=2007;$year<=2013;$year++){
      //begin month
      for ($month=1;$month<=12;$month++){
     $readthis1[]=$file;
//more code here

  }else if ($action=='write'){
    //loop through all the read values and write
    foreach ($readthis1 as $key=>$value){
//more code here
    }
    
    
    
     if($action=='read'){
    //end year
     }
      //end month
  }
  }
  }else if ($action=='write'){
  //end foreach
  }
    } 
    
?>

Open in new window





i am not looking to close the loops
$action='read';

if($action=='read'){
    //begin year
    for ($year=2007;$year<=2013;$year++){
           //begin month
           for ($month=1;$month<=12;$month++){
              $readthis1[]=$file;
           }
      }
}else if ($action=='write'){
    //loop through all the read values and write
    foreach ($readthis1 as $key=>$value){
    }
}

Open in new window



because I want to reuse 1000 lines of code
with if statement (read or write)
and I can not put into a function because I have many parameters
Avatar of Gary
Gary
Flag of Ireland image

As I said before your not opening/closing the For's and If's correctly - look here

      for ($month=1;$month<=12;$month++){
     $readthis1[]=$file;
//more code here

  }else if ($action=='write'){


You begin a for loop and without closing it you are doing an else if test for the opening if

Here you are closing the if statement but then doing an else if check for the if statement that has been closed

  }
  }else if ($action=='write'){



This is why you are getting errors.
and I can not put into a function because I have many parameters
What about having a single class as parameter which contains the information needed to process data?
I'm not sure I completely understand your problem, when you say you want to "reuse" the code, but it is possible that if you put the code you want to "reuse" into a seprate file and then  use the require statement to include that code in each place you want it.

Its also possible this won't work.  A better explanation of exactly what you want to do might help.
Avatar of rgb192

ASKER

I think this idea may work


write.php
<?php
    foreach ($readthis1 as $key=>$value){

include 'code.php';

  //end foreach
    } 
    
?>

Open in new window




read.php
<?php
    //begin year
    for ($year=2007;$year<=2013;$year++){
      //begin month
      for ($month=1;$month<=12;$month++){
     $readthis1[]=$file;

include 'code.php';
    //end month
     }
      //end year
  }
  
    
?>

Open in new window


code.php
<?php
//do something with the values
?>

Open in new window

That is how I was thinking you might use it.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of rgb192

ASKER

I created a function that echos value
because there were too many variables

thanks