在本地计算机上测试 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/444372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing a PHP script on a local computer
提问by ryeguy
New to PHP and web development in general. I am trying to get information from an HTML form to appear in a table on another web page after clicking submit. So I installed Apache and then PHP on my local PC and expected to be able to test a PHP script locally however it does not return the information I was expecting. The following is the code for the form:
PHP 和 Web 开发的新手。单击提交后,我试图从 HTML 表单中获取信息以显示在另一个网页的表格中。因此,我在本地 PC 上安装了 Apache,然后安装了 PHP,并希望能够在本地测试 PHP 脚本,但是它没有返回我期望的信息。以下是表格的代码:
<form method="post" action="showform.php">
Please fill out the following form if you would like to be contacted: <br/>
Name:<input type="text" name="name" /> <br/><br/>
Company: <input type="text" name="company"/> <br/><br/>
Phone: <input type="text" name="phone" /> <br/><br/>
Email: <input type="text" name="email" /> <br/><br/>
<input type="submit" name="Submit" value="Submit" />
</form>
The following is the code for the php script:
以下是php脚本的代码:
<table>
<tr><th>Field Name</th><th>Value(s)</th></tr>
<?php
if (empty($_POST)) {
print "<p>No data was submitted.</p>";
} else {
foreach ($_POST as $key => $value) {
if (get_magic_quotes_gpc()) $value=stripslashes($value);
if ($key=='extras') {
if (is_array($_POST['extras']) ){
print "<tr><td><code>$key</code></td><td>";
foreach ($_POST['extras'] as $value) {
print "<i>$value</i><br />";
}
print "</td></tr>";
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
}
}
?>
</table>
</body>
</html>
I know it works when used on the internet but how come it doesn't work locally. I have checked that apache and php are installed correctly. What could be the issue? The current result is a table with $key and $value in the places where the correct values should be, in other words in the table cells. Thanks for your help.
我知道它在互联网上使用时有效,但为什么它在本地不起作用。我已经检查过 apache 和 php 是否安装正确。可能是什么问题?当前结果是一个在正确值应该出现的位置(即表格单元格中)包含 $key 和 $value 的表格。谢谢你的帮助。
UPDATE: Now working though WAMPSERVER- thanks to all who helped!
更新:现在通过 WAMPSERVER 工作-感谢所有帮助过的人!
回答by ryeguy
Check out xampp. It installs Apache, Mysql, Perl, and PHP on your machine so you can test your entire site locally. It's a one shot installer and comes with a nifty control panel to enable/disable each service as needed.
查看xampp。它会在您的机器上安装 Apache、Mysql、Perl 和 PHP,以便您可以在本地测试整个站点。这是一个一次性安装程序,并带有一个漂亮的控制面板,可根据需要启用/禁用每项服务。
回答by xgMz
You could also try WampServer.
您也可以尝试WampServer。
It's a package containing: Apache, MySQL, PHP (Preconfigured for use in Windows).
它是一个包含:Apache、MySQL、PHP(预配置用于 Windows)的包。
回答by Harshi Srivastava
There's no need for a server if using PHP 5.5+ - it has a built-in server (http://www.php.net/manual/en/features.commandline.webserver.php)
如果使用 PHP 5.5+,则不需要服务器 - 它有一个内置服务器(http://www.php.net/manual/en/features.commandline.webserver.php)
Just use:
只需使用:
$ cd ~/project
$ cd ~/项目
$ php -S localhost:8000
$ php -S 本地主机:8000
回答by Ross
Sounds like your PHP script isn't being parsed if you're literally getting $value. Try running a basic phpinfo()to check this.
听起来你的 PHP 脚本没有被解析,如果你真的得到$value. 尝试运行一个基本phpinfo()来检查这一点。
If PHP doesn't pick that up make sure the path to PHP is set in httpd.conf. I'd reccommend using xampp though - I'm terrible at all the local configuration and just let the auto installer set it up for me. I know enough to add new modules etc. later on.
如果 PHP 没有选择,请确保 PHP 的路径设置在httpd.conf. 不过我建议使用 xampp - 我对所有本地配置都很糟糕,让自动安装程序为我设置它。我知道足够以后添加新模块等。
回答by Christopher Garcia
On first glance, it looks like you're trying to place variables, but you're putting the variables inside of a string.
乍一看,您似乎在尝试放置变量,但实际上是将变量放在字符串中。
So this:
所以这:
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
Should be something like:
应该是这样的:
print "<tr><td><code>" . $key . "</code></td><td><i>" . $value . "</i></td></tr>\n";

