2011-07-20 53 views
1

好吧所以即時通訊在這裏試圖練習一些PHP(即時超級初學者),所以長話短說, 我把表單元素放在一個頁面,將它傳遞給過程php。 我只是在試圖看看迄今爲止學到什麼。我沒有得到任何錯誤,只是不明白爲什麼它不工作。php課程/練習。需要幫助理解如果/其他

<?php 

$yourname = htmlspecialchars($_POST['name']); 
$compname = htmlspecialchars($_POST['compName']); 

$response = array("please enter correct information","hmm" . "$yourname"); 

function nametest() { 
if (!isset($yourname)){ 
    $yourname = $response[0];} 
    else { 
    $yourname = $response[1];; 
    } 
} 
?> 


<?php nametest(); ?> 

即時消息試圖做的是,如果名稱未設置,使一個變量等於響應內的值。

+1

你應該閱讀這個:http://php.net/manual/en/language.variables.scope.php –

回答

0

嘗試

function nametest() { 
    if (!isset($yourname)){ 
     $yourname = $response[0]; 
    } else { 
     $yourname = $response[1]; 
    } 
    return $yourname; 
} 
print nametest(); 

的函數需要返回要被打印的值。我也注意到你有兩個;;後面5行

+0

是的,下面的答案是正確的.. $ response不知道nametest(),你或者需要將其傳入nametest($ response)或將其聲明爲全局。 –

0

因爲你在頭兩行分配$yourname$compname

$yourname = htmlspecialchars($_POST['name']); 
$compname = htmlspecialchars($_POST['compName']); 

UPDATE您可以檢查這些在POST設置,因此以後不必檢查它們:

$yourname = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : "oops, no value"; 
$compname = isset($_POST['compName']) ? htmlspecialchars($_POST['compName']) : "oops, no value"; 

即使爲NULL或爲空,它們也將始終設置。所以,你後來致電isset()永遠是真的。 相反,你可以檢查它們是否爲空與empty()功能:根據意見更正

UPDATE沒有必要的。你的isset()應該工作。

// Check with empty() 
// but still won't work properly. keep reading below... 
function nametest() { 
if (!empty($yourname)){ 
    $yourname = $response[0];} 
    else { 
    $yourname = $response[1];; 
    } 
} 

但是,這裏還有另一個可變範圍的問題。變量是不可用的功能裏面,除非你要麼在將它們作爲參數,或者使用global關鍵字:

// $yourname is passed as a function argument. 
function nametest($yourname, $response) { 
    if (!empty($yourname)){ 
     $yourname = $response[0];} 
    else { 
     $yourname = $response[1];; 
    } 
} 

四處...現在你的函數分配$yourname,但它不會返回或打印任何值。添加一個return語句,然後你可以重複出結果:

function nametest($yourname, $response) { 
    if (!empty($yourname)){ 
     $yourname = $response[0];} 
    else { 
     $yourname = $response[1];; 
    } 

    // Add a return statement 
    return $yourname; 
} 

// Now call the function, echo'ing its return value 
echo nametest($yourname, $response); 
+1

根據[PHP.net docs'isset()'應該檢查NULL'](http://us3.php.net/manual/en/function.isset.php) – cspray

+0

你也在最後一行丟失了參數。 – zerkms

+0

我認爲,儘管我將$ yourname設置爲等於從prev頁面上的表單傳遞的數據,但這就是我正在檢查的內容,只要它是空的或不是。所以儘管我確實明白你對$ yourname的設置仍然等於$ _GET方法,那麼我應該檢查$ _GET本身?然後如果它已經設定好,那麼將它的價值賦予別的東西? – somdow

0

變量的作用域是最大的錯誤,在這裏,你的功能不能「看」,你在它之外創建的變量,這樣做:

<?php 
    . 
    . 
    . 
function nametest($yourname, $response) { // This creates two new variables that 
              // are visible only by this function 
    if (!isset($yourname)){ 
     $yourname = $response[0]; 
    } else { 
     $yourname = $response[1]; // Get rid of the extra semicolon 
    } 

    return $yourname; // This $yourname is only visible by this function so you 
          // need to send it's value back to the calling code 
} 
?> 


<?php nametest($yourname, $response); ?> // This sends the values of the 
              // variables that you created at the 
              // top of this script 
+0

您好,謝謝。好的,無論我嘗試使用哪個版本,我都會得到相同的結果,即 $ response = array(「請輸入正確的信息」,「welcome」。「,」。$ yourname); 用這一行,無論我嘗試使用哪個代碼塊,當我點擊表單上的提交按鈕時,沒有在名稱字段中輸入任何內容,在提供的所有代碼中,我都會得到響應[0],即「輸入正確的信息「,但我沒有,而是我得到」歡迎「。 然後當我重新加載頁面並輸入我的名字「somdow」,我得到「歡迎somdow」 是不是錯了?再次,無論我嘗試哪一種,我都會得到相同的結果。 – somdow

+0

您可以嘗試將isset($ yourname)更改爲$ yourname =='',它很可能在變量被傳遞時設置,即使它爲null,也可以嘗試使用empty($ yourname) –