2011-01-05 1 views
1

숨바꼭질 얘들 아에,치명적인 오류 : 정의되지 않은 함수 reg_form() C의에 전화 : XAMPP htdocs에 PHP 5부터 PHP는 mysql을 form_test.php 라인 (18)

내가 PHP에 새로 온 사람의 내가 언급 한 PHP 책 "함수는 실제 함수 정의가 코드에 나타나기 전에 호출 될 수 있습니다." 다음 코드에서는 함수가 아래에 정의되기 전에 _reg_form(); _을 호출하지만 오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까.

감사합니다.

<?php 
include('common_db.inc'); 
include('validation.php'); 

if(!$link=db_connect()) die(sql_error()); 
if($_POST['submit']) 
{ 
    $userid = $_POST['userid']; 
    $userpassword=$_POST['userpassword']; 
    $username=$_POST['username']; 
    $userposition = $_POST['userposition']; 
    $useremail=$_POST['useremail']; 
    $userprofile=$_POST['userprofile']; 
    $result=validate(); 
    if($result==0) 
    { 
    reg_form(); 
    } 
else 
{ 
    mysql_query("INSERT INTO user VALUES(NULL,'$userid',Password('$userpassword'),'$username','$userposition','$useremail','$userprofile')",$link); 
} 
} 
else 
{ 
?> 
<?php 
function reg_form() 
{ 
echo "<table border='1'> 
<form action='form_test.php' method='post'> 
    <tr><td>Desired ID:</td> 
    <td><input type='text' size='12' name='userid' /></td></tr> 
    <tr><td>Desired Password:</td> 
    <td><input type='password' size='12' name='userpassword' /></td></tr> 
    <tr><td><input type='hidden' name='submit' value='true' /> 
    <input type='submit' value='Submit' /> 
    <input type='reset' value='Reset' /></td></tr> 
</form> 
</table>"; 
} 

    reg_form(); 
?> 
<?php 
} 
?> 
+0

왜 함수를 먼저 정의하지 않습니까? 당신이하고있는 일이 가능한지 아닌지는 확실치 않습니다. (나는 추측 할 것입니다.) 그러나 어떤 상황에서도 끔찍한 코딩 스타일이 될 것이므로 실제로 중요하지 않습니다. – Hannes

답변

2

조건부 함수를 정의하고 있습니다. 단축

Functions need not be defined before they are referenced, except when a function is conditionally defined

귀하의 코드 :

if ($something) { 
    reg_form(); // use function (not defined) 
} else { 
    function reg_form() { 
     // define function only if (!$something) 
    } 
} 

따라서, 함수에만 다른 지점에 정의되어 The manual you are referring to이 (강조 광산)를 말한다. 다음과 같이 함수의 정의가 항상 실행되는 곳에서이 함수가 필요합니다.

if ($something) { 
    reg_form(); // use function (already defined since the script was loaded) 
} else { 
    // something else 
} 

// not conditional - will be loaded when script starts 
function reg_form() { 
    // define function 
} 
0

당신은 if() 블록의 else 절 내에서 함수를 정의하고 있습니다. 대부분의 경우 함수 호출은 다른 블록 중 하나에서 발생하는, 그래서이 함수는 호출시 아직 분석되지 않았을 것

if (..) { 
    reg_form(); 
} else { 
    function reg_form() { ... } 
} 

이 작동하지 않습니다. 함수는 코드의 최상위 레벨에서 정의되어야하며, 함수 나 논리 구조 외부에 정의되어야합니다.

0

함수 reg_form은 호출되는 범위 외부에서 정의됩니다.

+0

PHP는 이와 같은 기능 범위 개념을 가지고 있지 않습니다. 조건부로 정의 된 전역 함수는 여전히 전역 범위에 있지만 정의가 포함 된 코드 분기가 실행될 때까지는 존재하지 않습니다. – Piskvor

관련 문제