2013-01-01 1 views
2

내가 다음 코드 snipplet를 한 경우 및 단락 회로 :목록(), 평가

$active_from = '31-12-2009'; 
if(list($day, $month, $year) = explode('-', $active_from) 
    && !checkdate($month, $day, $year)) { 
    echo 'test'; 
} 

왜 내가 정의되지 않은 변수 에러가 발생합니까?

list($day, $month, $year) = explode('-', $active_from)true이므로 list()이 평가됩니까? 아닙니다. 변수가 정의되어야한다고 생각합니다. 내가 무엇을 감독합니까?

if((list($day, $month, $year) = explode('-', $active_from)) && checkdate($month, $day, $year)) { 

하지만 난 정말 왜 :-)

을 이해하지 않습니다

이 같은 내 의견 않고 오류가 발생하지 않습니다 :이 오류를 제기하지

$active_from = '31-12-2009'; 
list($day, $month, $year) = explode('-', $active_from); 
if(checkdate($month, $day, $year)) { 
    echo 'test'; 
} 

을 설명해 주셔서 감사합니다.

+2

여기에 오류가 없습니다. http://codepad.org/33BV3EsO –

답변

3

동일합니다.

할당 문을 괄호 안에 넣으면이 문제를 해결할 수 있습니다.

명시 적으로, 코드 내가 if(($a=$b) && $c)if($a=$b && $c)에서 변경 한

if( (list($day, $month, $year) = explode('-', $active_from)) 
    && !checkdate($month, $day, $year)) { 

주를 읽어야합니다. 괄호는 할당 연산자 (=)가 결합 (&&) 전에 평가되도록합니다. 이는 원하는 것입니다.

+0

대단히 감사합니다. –

+0

문제 없습니다, @FabianBlechschmidt. (그리고 새해 복 많이 받아 ;-)) – Richard

1

operator precedence에 대해 읽어보십시오.

if (list($day, $month, $year) = explode('-', $active_from) && !checkdate($month, $day, $year)) { 

이것은, operator precedence의 문제입니다 귀하의 경우, && 당신이 설명하는 오류를지도하는 = 전에 평가

if (list($day, $month, $year) = (explode('-', $active_from) && !checkdate($month, $day, $year))) { 
+0

대단히 감사합니다. :) –