Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的php代码赢得';你不能用HTML运行吗?_Php_Html - Fatal编程技术网

我的php代码赢得';你不能用HTML运行吗?

我的php代码赢得';你不能用HTML运行吗?,php,html,Php,Html,我正在一个简单的web表单问题上练习php。长话短说,我只需要创建一个非常基本的web表单,它包含欧洲城市的二维数组,以公里为单位显示它与其他欧洲城市的距离。我所需要做的就是创建一个web表单,在该表单中,您可以输入在二维数组中找到的任何城市,并让它计算起点城市和终点城市之间的距离(即,在文本字段中输入“Berlin”和“Prague”后,Berlin位于离布拉格不远的地方) 我想我的问题几乎解决了,但问题是phpstorm告诉我某些变量是未定义的,即使它们已经在我的函数中定义了,并且在该函数之

我正在一个简单的web表单问题上练习php。长话短说,我只需要创建一个非常基本的web表单,它包含欧洲城市的二维数组,以公里为单位显示它与其他欧洲城市的距离。我所需要做的就是创建一个web表单,在该表单中,您可以输入在二维数组中找到的任何城市,并让它计算起点城市和终点城市之间的距离(即,在文本字段中输入“Berlin”和“Prague”后,Berlin位于离布拉格不远的地方)

我想我的问题几乎解决了,但问题是phpstorm告诉我某些变量是未定义的,即使它们已经在我的函数中定义了,并且在该函数之前是一个变量。如果这个问题看起来微不足道,我很抱歉,但我现在正在学习理解php。下面是我的代码。我包括了html和php代码放在一个页面中

<!DOCTYPE html>
<html lang="en">
<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";
        } else {
            echo "<p>$City1 and $City2 are not in this list.</p>\n";
        }
    }
}
}
?>
<form action= "eudistance.php" method="post">

City 1: <input type="text" name="firstCity" /><br />

City 2: <input type="text" name="secondCity" /><br />

<input type="reset" value="Clear All" />

<input type="submit" name="submit" value="Enter" />

</form>
</body>
</html>

欧洲城市之间的距离
欧洲城市之间的距离
在此列表中输入有效的欧洲城市:柏林、莫斯科、巴黎、布拉格、罗马
城市1:
城市2:

您的php代码首先执行,因此会出现一些错误。只需添加

if(isset($_POST['submit'])){
...
}
在php代码中,这样php代码只有在提交表单详细信息后才会执行

$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
以上两个变量没有初始化,它们是从表单中获取值,因此在执行php代码之前需要提交表单数据。您应该调用函数来计算距离

函数调用:

if(isset($_POST['submit']))
{
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
euDis(trim($City1),trim($City2));  //function calling
}
像这样:

<!DOCTYPE html>
<html lang="en">
<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
if(isset($_POST['submit']))
{
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
euDis(trim($City1),trim($City2));
}
function euDis($City1, $City2){
$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));


if (empty(trim($City1)) || empty(trim($City2)))  {
    echo "Please input two cities in the required fields.<br />\n";
} 
 else {
        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";} 
        else {
            echo "<p>$City1 and $City2 are not in this list.</p>\n";
        }    }
}
?>

<form action= "" method="post">

City 1: <input type="text" name="firstCity" /><br />

City 2: <input type="text" name="secondCity" /><br />

<input type="reset" value="Clear All" />

<input type="submit" name="submit" value="Enter" />

</form>
</body>
</html>

欧洲城市之间的距离
欧洲城市之间的距离
在此列表中输入有效的欧洲城市:柏林、莫斯科、巴黎、布拉格、罗马
城市1:
城市2:

我改变了以下事情

  • 你没有调用你的函数
  • 城市名称是数组的
    key
    。\u数组中的
    返回
    true
    如果变量是数组的成员,而不是key
  • 
    欧洲城市之间的距离
    欧洲城市之间的距离
    在此列表中输入有效的欧洲城市:柏林、莫斯科、巴黎、布拉格、罗马
    城市1:
    城市2:

    代码中有两个问题。第一个问题是在分配post变量之前检查数据是否可用。第二个问题是距离数组中可用的城市

    已修复您的问题。请尝试此代码一次

    <!DOCTYPE html>
    <html lang="en">
    <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;
    if(isset($_POST['submit'])){
        $City1 = $_POST['firstCity'];
        $City2 = $_POST['secondCity'];
    
        if (empty($City1) || empty($City2)) {
            echo "Please input two cities in the required fields.<br />\n";
        } elseif (is_array($Distances[$City1]) == False || is_array($Distances[$City2])== 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";
                } else {
                    echo "<p>$City1 and $City2 are not in this list.</p>\n";
                }
            }
        }
    
    }
    ?>
    <form action= "" method="post">
    
    City 1: <input type="text" name="firstCity" /><br />
    
    City 2: <input type="text" name="secondCity" /><br />
    
    <input type="reset" value="Clear All" />
    
    <input type="submit" name="submit" value="Enter" />
    
    </form>
    </body>
    </html>
    
    
    欧洲城市之间的距离
    欧洲城市之间的距离
    在此列表中输入有效的欧洲城市:柏林、莫斯科、巴黎、布拉格、罗马
    城市1:
    城市2:

    实际发生了什么?是否有错误?当我在浏览器上运行它时,会出现502错误。当我在phpstorm上运行它时,它会告诉我“PHP通知:未定义索引:C:\Users\user\PhpstormProjects\eudistance.PHP第19行中的firstCity PHP通知:未定义索引:C:\Users\user\PhpstormProjects\eudistance.PHP第20行中的secondCity”我认为我的参数是通过前两个变量定义的,这两个变量从输入的文本字段中检索文本。PHP代码不会做任何事情。它只是定义和设置变量
    $distance
    $KMtoMiles
    $City1
    $City2
    ,以及一个函数
    euDis
    ,其他什么都没有n不会在任何地方调用。此外,当第一次执行时(不向其发送表单数据),您将读取未定义的索引
    $\u POST['firstCity']
    $\u POST['secondCity']
    因为
    $\u POST
    数组是空的。html代码必须在php代码之前插入吗?或者我只是误解了这些变量是如何工作的?你修复了未定义的索引问题,但这段代码仍然没有做任何事情。是的,它可以工作。如果我想,我可以使用一些elseif语句对其进行压缩,对吗?是的,你可以使用elseif语句。谢谢。这也很有效,看起来也很整洁。我应该养成将php和html代码分开的习惯。是的,如果你这样做,你可以很容易地理解错误。在html中添加php代码应该只是为了显示一些值,所以你的代码会变得更干净。如果我能帮助你,我很高兴。
    <!DOCTYPE html>
    <html lang="en">
    <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;
    if(isset($_POST['submit'])){
        $City1 = $_POST['firstCity'];
        $City2 = $_POST['secondCity'];
    
        if (empty($City1) || empty($City2)) {
            echo "Please input two cities in the required fields.<br />\n";
        } elseif (is_array($Distances[$City1]) == False || is_array($Distances[$City2])== 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";
                } else {
                    echo "<p>$City1 and $City2 are not in this list.</p>\n";
                }
            }
        }
    
    }
    ?>
    <form action= "" method="post">
    
    City 1: <input type="text" name="firstCity" /><br />
    
    City 2: <input type="text" name="secondCity" /><br />
    
    <input type="reset" value="Clear All" />
    
    <input type="submit" name="submit" value="Enter" />
    
    </form>
    </body>
    </html>