我現(xiàn)在正在練習(xí) php 解決一個(gè)簡(jiǎn)單的 Web 表單問(wèn)題。長(zhǎng)話短說(shuō),我只需要?jiǎng)?chuàng)建一個(gè)非?;镜?Web 表單,該表單接受歐洲城市的二維數(shù)組,以公里為單位顯示它與其他歐洲城市的距離。我需要做的就是創(chuàng)建一個(gè) Web 表單,您可以在其中輸入在該二維數(shù)組中找到的任何城市,并讓它計(jì)算起始城市和結(jié)束城市之間的距離(即,在輸入“柏林”和“布拉格”在文本字段中。我想我的問(wèn)題幾乎解決了,但問(wèn)題是 phpstorm 告訴我某些變量未定義,即使它們已經(jīng)在我的函數(shù)中定義并且在該函數(shù)之前作為變量。如果這個(gè)問(wèn)題看起來(lái)微不足道,我深表歉意,但我目前只是在學(xué)習(xí)消化 php。下面是我的代碼。我在一頁(yè)中包含了 html 和 php 代碼。<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Distance between European Cities</title></head><body><h1>Distance between European Cities</h1><h2>Enter valid European cities from this list: Berlin, Moscow, Paris, Prague, Rome</h2><?php$Distances = array( "Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" => 1181.67), "Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" => 2374.26), "Paris" => array("Berlin" => 876.96, "Moscow" => 641.34, "Paris" => 0, "Prague" => 885.38, "Rome" => 1105.76), "Prague" => array("Berlin" => 280.34, "Moscow" => 1607.99, "Paris" => 885.38, "Prague" => 0, "Rome" => 922), "Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" => 0));$KMtoMiles = 0.62;$City1 = $_POST['firstCity'];$City2 = $_POST['secondCity'];function euDis($City1, $City2, $Distances){if (empty($City1) || empty($City2)) { echo "Please input two cities in the required fields.<br />\n";} elseif (in_array($City1, $Distances) == FALSE || in_array($City2, $Distances) == FALSE) { echo "You inputted one or more cities that are not in our list.<br />";} else { if (isset($_POST['submit'])) { $City1 = stripslashes($_POST['firstCity']); $City2 = stripslashes($_POST['secondCity']); if (isset($Distances[$City1][$City2])) { echo "<p>The distance from $City1 to $City2 is " . $Distances[$City1][$City2] . " kilometers or " . round((0.62 * $Distances[$City1][$City2]), 2) . " miles.</p>\n"; }
- 3 回答
- 0 關(guān)注
- 214 瀏覽
添加回答
舉報(bào)
0/150
提交
取消