【发布时间】:2013-09-27 11:05:08
【问题描述】:
我正在使用一个数组在 foreach 语句中记录关键信息以供以后使用,我将数组设置在页面顶部以确保它在脚本中的所有函数中都可用。
我已经测试了该数组是否已设置,并且它似乎在 try catch 语句之外工作,请参阅下面的注释代码。
$completeClients = array();
if($can_synchronise === true)
{
$success = sync_to_client($package, $client);
try
{
if($success)
{
$completeClients[] = "Sync to ".$client->getName()." has completed";
}
else
{
$completeClients[] = "Sync to ".$client->getName()." has failed";
}
}
catch(Exception $ex)
{
logMsg("Unable to save client data reason: ". $ex->getMessage(), STATUS_ERROR , $client->getAddress());
}
exit( EXIT_OK );//Exit the child process
}
}
else
{
**// The array is set correctly when called here**
$completeClients[] = "Sync to ".$client->getName()." has failed";
}
我已经打印了数组,当它在 try catch 语句中被调用时,它看起来像:
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
)
Array
(
[0] => Sync to TPSDEV_TC_Client2 has completed
)
Array
(
)
应该是这样的
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
)
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
[1] => Sync to TPSDEV_TC_Client2 has completed
)
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
[1] => Sync to TPSDEV_TC_Client2 has completed
)
你们有什么想法吗?我被难住了。
【问题讨论】:
-
看来你在
exit( EXIT_OK );之后多了一个大括号} -
@hallaji:不,这是第一行
if语句的结束}。 OP 使用的是 Allman 编码风格(有点与 Whitesmiths 风格交叉) -
致 OP:你问了 6 个问题,接受了 0 个答案?请consider accepting an answer
标签: php arrays foreach try-catch