MikZuit
MikZuit

Reputation: 734

Variables not defined inside function on second time at foreach

Considering all this files :

vars.php

if($local){
    $var = 'var';
    $foo = 'foo';
    $var1 = 'var1';
    $foo1 = 'foo1';
}else{
    $var = '';
    $foo = '';
    $var1 = '';
    $foo1 = '';
}

/remote/vars.remote.php is the same as vars.php but different values for variables

cons.php

 $local = isset($_SERVER['REMOTE_ADDR']) && ( $_SERVER['REMOTE_ADDR'] === '127.0.0.1') ? 1 : 0;
 if ($local){
      include_once ('vars.php');
 }else{
       include_once('/remote/vars.remote.php);
 }    
 define(CONST_OP_1,$var);
 define(CONST_MAIL_1,$foo);
 define(CONST_OP_2,$var1);
 define(CONST_MAIL_2,$foo1);

config.php

 require_once "Mail/Queue.php"; 
 include_once ('cons.php');
 $db_options['user'] = CONST_OP_1;
 $db_options['pass'] = CONST_OP_2;
 $mail_options['port'] = CONST_MAIL_1;
 $mail_options['dsn'] = CONST_MAIL_2;

mail.php

 class Sendmail
   {
 ...
function sendc($var){
      require_once ('config.php');
      $mail_queue = new Mail_Queue( $db_options , $mail_options );
 }

...

}

comm.php

 foreach($array_mails as $email){
    $mail_q =  $sendmail->sendc($u_name);
}

There is something I do not understand and I'm not able to figured this out why it happens . when I execute comm.php with and ajax function I pass and array in $array_mails but for a strange reason to me , everything work just fine at the first loop but at second loop (and following) at the foreach it seem that arrays inside config.php file does not seem to be declared so it give me a

NOTICE: undefined variables for $db_options and $mail_options.

I know how to fix this but I would like to understand why is this happens. Can anyone explain please ?

Upvotes: 3

Views: 851

Answers (1)

ixchi
ixchi

Reputation: 2389

You need to change the require_once('config.php') to a require, or alternatively place it at the top of the file.

require_once only allows the file to be included a single time, so it doesn't include it again for the second loop.

Upvotes: 3

Related Questions