Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我可以在foreach变量的作用域之外访问它?_Php_Php 5.6 - Fatal编程技术网

Php 为什么我可以在foreach变量的作用域之外访问它?

Php 为什么我可以在foreach变量的作用域之外访问它?,php,php-5.6,Php,Php 5.6,我注意到我可以在变量范围之外访问变量$order的值 public function dryRunAction() { $allCustomersBefore = Mage::getModel('customer/customer')->getCollection() ->addAttributeToSelect('*')

我注意到我可以在变量范围之外访问变量
$order
的值

public function dryRunAction()
{     
    $allCustomersBefore = Mage::getModel('customer/customer')->getCollection()
                                                       ->addAttributeToSelect('*')
                                                       ->addFieldToFilter('customer_activated', '1')
                                                       ->addFieldToFilter('group_id', array('6'));


    foreach($allCustomersBefore as $customer) {

        $orders = Mage::getResourceModel('sales/order_collection')
            ->addAttributeToSelect('*')
            ->addFieldToFilter('customer_id', $customer->getId());

        $atLeastOnePendingOrder = false;
        foreach($orders as $order) {

            if ($order->getStatus() == 'pending') {
                $atLeastOnePendingOrder = true;
                break;
            }
        }


        if ($atLeastOnePendingOrder) {
            echo $customer->getName() . " already made an order (Order Status: ". $order->getStatus() . ").<br>";        
        }
    }
}
公共函数dryRunAction()
{     
$allCustomersBefore=Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect(“*”)
->addFieldToFilter('customer_activated','1')
->addFieldToFilter('group_id',array('6'));
foreach($allCustomersBefore作为$customer){
$orders=Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect(“*”)
->addFieldToFilter('customer_id',$customer->getId());
$atLeastOnePendingOrder=false;
foreach($orders作为$order){
如果($order->getStatus()=='pending'){
$atLeastOnePendingOrder=true;
打破
}
}
如果($atLeastOnePendingOrder){
echo$customer->getName()。“已下订单(订单状态:“.$order->getStatus()”。
”; } } }
在foreach之前未定义变量$output


输出为
Mr示例客户已下订单(订单状态:待定)。

foreach循环是一个块级别,而不是功能级别

函数中声明的变量在外部不可用

但是,块外的变量总是可用的

它们的值应该是迭代中的最新值


因为代码是同步的,所以它会在循环中进行定义的最后一次迭代。。。