如何使用 vscode 运行 PHP

Posted

技术标签:

【中文标题】如何使用 vscode 运行 PHP【英文标题】:How to run PHP with vscode 【发布时间】:2020-04-19 14:41:50 【问题描述】:

我正在尝试创建一个简单的 html 表单,将用户重定向到服务器显示的感谢消息,并用 php 编写。但是,我所做的一切似乎都不起作用。每次尝试运行代码时都会收到 405。如果我刷新页面,它将开始下载。我正在使用实时服务器扩展并安装了 PHP。

HTML 文件:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel = "stylesheet" type = "text/css" href = "styles-signup.css">
    <script type = "text/javascript" src = "script-signup.js"></script>

</head>
<body>
    <div>
        <h1 class = "title"></h1>
    </div>
    <div class = "form">
        <h1 id = "header">Create Your Account</h1>
        <form method = "POST" id = "signup" action = "action.php">
            <input name = "firstName" type = "text" placeholder="First Name" required id = "fname">
            <input name = "lastName" type = "text" placeholder = "Last Name" required id = "lname">
            <input name = "Email" type = "email" placeholder="email, e.g. person@gmail.com" required id = "mail">
            <input name = "newPassword" type = "password" id = "password" placeholder="Password" required minlength="8" maxlength="25">
            <input name = "passwordConfirm" type = "password" placeholder = "Confirm Password" required minlength="8" maxlength="25" id = "cpassword">
           <br>

           <input name = "gender" type = "radio" id = "male" required>
           <label for = "gender" id = "mlabel">Male</label>

           <input name = "gender" type = "radio" id = "female" required>
           <label for = "gender" id = "flabel">Female</label>

           <input name = "gender" type = "radio" id = "other" required>
           <label for = "gender" id = "olabel">Other</label>
           <br>

           <br>
           <input name = "birthdate" type = "number" min = "1920" max = "2019" placeholder="Year Of Birth" id = bd required>
           <br>

            <input name = "terms" type ="checkbox" id = "box" required>

            <p id = "tc">I agree to the <a href = "termsandconditions.txt" id = "tcLink">Terms and Conditions</a></p>

            <input name = "submit" type = "Submit" id = "button" value = "Sign up">

            <a href = "login.html" id = "login">Already have an account? Log in</a>

        </form>
    </div>

</body>
</html>

PHP 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <?php
    echo "<p>Hi</p>";
    ?>

</body>
</html>

我收到此错误:

【问题讨论】:

欢迎。没有任何代码是不可能帮助你的。您必须最终缩小问题范围,然后提供minimal, reproducible example。 VSCode 只是一个编辑器。它无法执行您的 PHP 代码。您需要 PHP 可执行文件来执行它。 您目前使用的是什么平台,Windows 还是 Linux? 【参考方案1】:

您需要设置一个服务器才能使用 PHP。如果您使用 Windows,它们是几个易于使用的选项,请尝试下载其中一个:

    XAMPP WAMP

如果您使用的是 Linux,则需要构建 LAMP 服务器。如果你在mac上,我认为它被称为OS-X。这听起来像是你对这一切的新手。我强烈建议设置一个服务器,它会真正帮助您了解网站的工作原理,并让您能够编写和学习服务器端语言,如 PHP 和 ASP。

请注意,服务器会中断 PHP,而不是编辑器和 IDE。

【讨论】:

当我在 windows 上下载了 xampp 后,是否需要对 Vs 代码进行任何额外的配置才能使其工作? VS 代码只是一个编辑器,它不会解析或解释代码,所以你不需要对 VS 代码做任何额外的事情。您必须在 XAMPP 根目录中构建您的项目才能使其正常工作。

以上是关于如何使用 vscode 运行 PHP的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Visual Studio Code (VSCode) 上运行或调试 php

VSCODE 中的 PHP 7 Xdebug:如何忽略异常

VSCode如何运行node文件

VSCode Prettier 没有格式化 PHP

如何配置 VSCode 以运行 Yarn 2(使用 PnP)驱动的 TypeScript

vscode如何使用模拟器运行flutter项目