在foreach第二次没有定义函数内部的变量

时间:2015-04-27 14:13:59

标签: php arrays foreach

考虑所有这些文件:

vars.php

2015-04-27T13:49:27.160198+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/mmbu-timesheets -Dhttp.port=80`
2015-04-27T13:49:29.321552+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8 -Djava.rmi.server.useCodebaseOnly=true
2015-04-27T13:49:29.898379+00:00 app[web.1]: Play server process ID is 3
2015-04-27T13:49:32.549840+00:00 app[web.1]: [[37minfo[0m] application - mongodb connection ds031701.mongolab.com:31701 db->heroku_app36286493
2015-04-27T13:49:33.374954+00:00 app[web.1]: [[37minfo[0m] play - Application started (Prod)
2015-04-27T13:49:33.628067+00:00 app[web.1]: Oops, cannot start the server.
2015-04-27T13:49:33.630568+00:00 app[web.1]:    at play.core.server.NettyServer$$anonfun$8.apply(NettyServer.scala:89)
2015-04-27T13:49:33.630523+00:00 app[web.1]:    at play.core.server.NettyServer$$anonfun$8.apply(NettyServer.scala:92)
2015-04-27T13:49:33.630859+00:00 app[web.1]:    at play.core.server.NettyServer$.createServer(NettyServer.scala:206)
2015-04-27T13:49:33.630358+00:00 app[web.1]: org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:80
2015-04-27T13:49:33.630483+00:00 app[web.1]:    at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:272)
2015-04-27T13:49:33.630606+00:00 app[web.1]:    at scala.Option.map(Option.scala:146)
2015-04-27T13:49:33.630802+00:00 app[web.1]:    at play.core.server.NettyServer.<init>(NettyServer.scala:89)
2015-04-27T13:49:33.630898+00:00 app[web.1]:    at play.core.server.NettyServer$$anonfun$main$3.apply(NettyServer.scala:243)
2015-04-27T13:49:33.630941+00:00 app[web.1]:    at play.core.server.NettyServer$$anonfun$main$3.apply(NettyServer.scala:238)
2015-04-27T13:49:33.631096+00:00 app[web.1]:    at play.core.server.NettyServer$.main(NettyServer.scala:238)
2015-04-27T13:49:33.630975+00:00 app[web.1]:    at scala.Option.map(Option.scala:146)
2015-04-27T13:49:33.631150+00:00 app[web.1]:    at play.core.server.NettyServer.main(NettyServer.scala)
2015-04-27T13:49:33.633017+00:00 app[web.1]: Caused by: java.net.SocketException: Permission denied
2015-04-27T13:49:33.633065+00:00 app[web.1]:    at sun.nio.ch.Net.bind0(Native Method)
2015-04-27T13:49:33.633136+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:437)
2015-04-27T13:49:33.633179+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:429)
2015-04-27T13:49:33.633235+00:00 app[web.1]:    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
2015-04-27T13:49:33.633396+00:00 app[web.1]:    at org.jboss.netty.channel.socket.nio.NioServerBoss$RegisterTask.run(NioServerBoss.java:193)
2015-04-27T13:49:33.633444+00:00 app[web.1]:    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.processTaskQueue(AbstractNioSelector.java:372)
2015-04-27T13:49:33.633550+00:00 app[web.1]:    at org.jboss.netty.channel.socket.nio.NioServerBoss.run(NioServerBoss.java:42)
2015-04-27T13:49:33.633600+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
2015-04-27T13:49:33.633643+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
2015-04-27T13:49:33.633305+00:00 app[web.1]:    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
2015-04-27T13:49:33.633525+00:00 app[web.1]:    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:296)
2015-04-27T13:49:33.633703+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:745)
2015-04-27T13:49:34.926567+00:00 heroku[web.1]: Process exited with status 255
2015-04-27T13:49:34.945535+00:00 heroku[web.1]: State changed from starting to crashed

cons.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

的config.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);

mail.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;

comm.php

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

...

}

有一些我不理解的东西,我无法弄清楚它为什么会发生。当我执行 foreach($array_mails as $email){ $mail_q = $sendmail->sendc($u_name); } comm.php时,我在ajax function传递和数组但由于一个奇怪的原因,一切都在第一个循环中正常工作但在第二个循环(及其后)处于看来$array_mails文件中的数组似乎没有被声明,所以它给了我一个

  

注意:$ db_options和$ mail_options的未定义变量。

我知道如何解决这个问题,但我想了解为什么会发生这种情况。有人可以解释一下吗?

1 个答案:

答案 0 :(得分:2)

您需要将require_once('config.php')更改为require,或者将其放在文件的顶部。

require_once仅允许一次包含文件,因此第二次循环不再包含该文件。