我的 php 代码不能与 HTML 一起运行有什么原因吗?

我现在正在练习 php 解决一个简单的 Web 表单问题。长话短说,我只需要创建一个非常基本的 Web 表单,该表单接受欧洲城市的二维数组,以公里为单位显示它与其他欧洲城市的距离。我需要做的就是创建一个 Web 表单,您可以在其中输入在该二维数组中找到的任何城市,并让它计算起始城市和结束城市之间的距离(即,在输入“柏林”和“布拉格”在文本字段中。


我想我的问题几乎解决了,但问题是 phpstorm 告诉我某些变量未定义,即使它们已经在我的函数中定义并且在该函数之前作为变量。如果这个问题看起来微不足道,我深表歉意,但我目前只是在学习消化 php。下面是我的代码。我在一页中包含了 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";

        } 

紫衣仙女
浏览 191回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP