簡體   English   中英

從輸入的出生日期計算年齡時的不確定索引

[英]Undefined index when calculating age from an inputted birthdate

我是該站點的新手,我發現了一些與系統錯誤有關的問題,但不幸的是它們無法解決該錯誤。 is undefined.. Here is my code 我正在為我的頂點項目創建一個基於Web的脫機信息系統,但我不明白為什么未定義 。這是我的代碼

這是我輸入生日的代碼:

input type="text" id = "P_Bday" name = "P_Bday" class="form-control" data-inputmask="'alias': 'dd/mm/yyyy'" data-mask placeholder="dd/mm/yyyy" required

這是我的年齡計算代碼:

function ageCalculator($dob){
    if(!empty($dob)){
        $birthdate = new DateTime($dob);
        $today   = new DateTime('today');
        $age = $birthdate->diff($today)->y;
        return $age;
    }
    else{
        return 0;
    }
}

$dob = $_POST["P_Bday"];

我在這里調用函數,該函數應根據輸入的出生日期顯示計算出的年齡:

input type='text' name = 'P_Age' id='disabledTextInput' class='form-control' value='".ageCalculator($dob)."' readonly

每當我運行我的代碼時,它都會說:

通知:未定義指數:P_Bday在C:\\ XAMPP \\ htdocs中\\ PISGDH \\ recordclerk \\在線47RecordEntry \\ addPatient.php

如果行$dob = $_POST["P_Bday"]; 在通過POST發送任何內容之前正在頁面上運行,則$_POST[foo]無效。

將行更改為:

if(isset($_POST["P_Bday"])) $dob = $_POST["P_Bday"];
    else $dob = null;

要么:

$dob = isset($_POST["P_Bday"]) ? $_POST["P_Bday"] : null;

Undefined index錯誤非常易於調試。 您從錯誤消息C:\\xampp\\htdocs\\PISGDH\\recordclerk\\RecordEntry\\addPatient.php提到的文件開始,然后轉到錯誤消息line 47行中提到的line 47並在該行P_Bday和以下P_Bday找到有問題的未定義索引絕對可以肯定的是,到目前為止,在代碼中您還沒有為該變量定義該索引。 您可以向后瀏覽代碼,以嘗試找出錯誤。 錯誤可能是拼寫錯誤(您使用了錯誤的大小寫/變量名稱),也可能是您忘記了正確初始化變量。

避免未定義的變量/索引錯誤的最佳方法是始終初始化並盡早初始化 在少數情況下,您不能確定變量是否已正確初始化( 例如,在客戶端輸入的控制下使用$_POST / $_GET或其他外部變量 ),您可以使用isset來避免錯誤,並且可以合並空值或編寫邏輯,以防止用戶出錯時代碼以未初始化的值繼續。

if (!isset($_POST['P_Bday'])) {
    die("You forgot to fill out your birthday!");
} else {
    echo "Yay!";
}

$_POST / $_GET一些好的初始化技術

在處理用戶輸入時,“ 始終初始化並盡早初始化 ”的一個好的最佳實踐是為表單中的預期輸入設置默認值集,並從中進行初始化,以免落入此陷阱。

$defaultValues = [
    'P_Bday'  => null,
    'Option1' => 'default',
    'Option2' => 1,
];
/* Let's say the user only supplied Option1 */
$_POST = ['Option1' => 'foo'];
/* This makes sure we still have the other index initialized */
$inputValues = array_intersect_key($_POST, $defaultValues) + $defaultValues;

/**
 * Now you can pass around $inputValues safely knowing all expected values
 * are always going to be initialized without having to do isset() everywhere
 */

doSomething(Array $inputValues) {
    if (!$inputValues['P_Bday']) { // notice no isset() check is necessary
        throw new Exception("You didn't give a birthday!!!");
    }
    return (new DateTime)->diff(new DateTime($inputValues['P_Bday']))->y;
}

您在調用函數后聲明了變量$ dob。 您必須在函數調用之前聲明變量,還必須使用如下條件語句:請按如下方式編寫代碼:

if(isset($_POST["P_Bday"])){
    $dob = $_POST["P_Bday"];
} else {
    $dob ="";
}
function ageCalculator($dob){
    if(!empty($dob)){
        $birthdate = new DateTime($dob);
        $today   = new DateTime('today');
        $age = $birthdate->diff($today)->y;
        return $age;
    }
    else{
        return 0;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM