2013-03-25 6 views
1

foreach() 루프를 수행하면 현재 배열 요소의 값 $recipient->to($recipient) 라인에 정의되어 있지 않습니다. 왜 이런거야?foreach 루프에서 정의되지 않은 변수

PHP 코드는 당신이 놓친

foreach($recipients as $recipient) { 
    echo $recipient; 
} 

답변

3

foreach($recipients as $recipient) { 
    Mail::send('emails.invite', $data, function($m){ 
     $m 
      ->from('[email protected]', Auth::user()->name) 
      ->to($recipient) 
      ->subject('Auth::user()->name has invited you!'); 
    }); 
} 

오류

Notice: Undefined variable: recipient 

PHP 코드 (오류) (오류가 발생합니다) use 키워드 코드를 다음으로 변경하십시오.

foreach($recipients as $recipient) { 
    Mail::send('emails.shareListing', $data, function($m) use($recipient) { 
     $m 
      ->from('[email protected]', Auth::user()->name) 
      ->to($recipient) 
      ->subject('Auth::user()->name has shared a listing with you!'); 
    }); 
} 

this documentation - 특히 세 번째 예를 참조하십시오. 제품 견적 :

클로저는 부모 범위에서 변수를 상속 할 수도 있습니다. 이러한 변수는 함수 헤더에 선언해야합니다.

+0

감사합니다! – Nyxynyx

+0

당신은 환영합니다! 한 번 문제가있었습니다. PHP 문서는 이것에 대해 많이 말하지 않는다. – hek2mgl

1

당신이 기능의 범위에 있기 때문입니다. 당신이 모든 기능을 전달하는 이유

당신이 여기에 PEAR 패키지를 사용하는 가정, 이해가 안 :이 일을 할 의미하는 경우가 전달하는 use 키워드를 사용할 수 있습니다 http://pear.php.net/manual/en/package.mail.mail.send.php

함수 범위에 변수 :

function($m) use($recipient) { 
관련 문제