wordpress
  • php
  • 2011-12-02 130 views 1 likes 
    1

    可能重複:
    PHP: 「Notice: Undefined variable」 and 「Notice: Undefined index」未定義指數錯誤信息

    我是新來的PHP,並玩了。我在我的PHP文件中有以下代碼。

    $output = "<div style='display:none'> 
        <div class='contact-top'></div> 
        <div class='contact-content'> 
         <h1 class='contact-title' style='text-align:center'>Write a Testimonial:</h1> 
         <div class='contact-loading' style='display:none'></div> 
         <div class='contact-message' style='display:none'></div> 
         <form action='#' style='display:none'> 
          <label for='contact-name'>*Name:</label> 
          <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' /> 
          <label for='contact-email'>*Email:</label> 
          <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />"; 
    
        if ($extra["form_subject"]) { 
         $output .= " 
          <label for='contact-subject'>Subject:</label> 
          <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />"; 
        } 
    
        $output .= " 
          <label for='contact-message'>*Message:</label> 
          <textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea> 
          <br/>"; 
    
        if ($extra["form_cc"]) { 
         $output .= " 
          <label>&nbsp;</label> 
          <input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span> 
          <br/>"; 
        } 
    
        $output .= " 
          <label>&nbsp;</label> 
          <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button> 
          <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button> 
          <br/> 
          <input type='hidden' name='token' value='" . smcf_token($to) . "'/> 
         </form> 
        </div> 
    
    </div>"; 
    
        echo $output; 
    

    當我試圖運行的代碼,雖然模型框出現,但也呈現出一個PHP錯誤。

    下面

    是錯誤消息我收到

    注意:未定義指數:form_cc在F:\ WAMP \ WWW \博客\ WordPress的\可溼性粉劑內容\插件\演示\上線56 contact.php

    任何想法是什麼問題?

    回答

    2

    的價值,因爲陣列extra沒有索引稱爲form_cc 。做一個var_dump的數組extra,所以你可以看到你的問題在哪裏。也可以使用isset()empty()方法。

    2

    簡單,看來你沒有這個指數在數組中的定義,在這種情況下,似乎你沒有得到輸入

    2

    在這種情況下簡單地使用:

    if (!empty($extra["form_cc"])) { 
        ... 
    } 
    

    空()將檢查陣列中的鍵/索引的存在和值也是否被設置或沒有。

    2

    問題是在$extra數組中你沒有一個名爲「form_cc」的位置(索引)。因此,你應該使用

    if (! empty($extra["form_cc"])) 
    { 
        // do stuff 
    } 
    
    0
    if (isset($extra["form_cc"])) { 
        ... 
    } 
    

    將刪除警告信息,這是一個很好的習慣,檢查isset()函數

    相關問題