php 如何让PHP脚本在浏览器中运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11144641/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:50:12  来源:igfitidea点击:

How to get PHP script to run in browser?

php

提问by user1459268

Possible Duplicate:
PHP script not running on browser

可能重复:
PHP 脚本未在浏览器上运行

I'm trying to work with PHP and am very new at it. I'm trying to just test the waters with a simple hello world program. I've tried it like this:

我正在尝试使用 PHP,并且对它非常陌生。我正在尝试使用一个简单的 hello world 程序来测试水域。我试过这样:

<html>
<body>

<h>Php File</h>

<?php echo "hello world";?>

<p>Did it work?</p>

</body>
</html>

and then opening the html file in my browser (currently chrome). Only the did it work?part shows in the browser. Not the actual PHP stuff I'm trying to run. Any ideas?

然后在我的浏览器(当前为 chrome)中打开 html 文件。did it work?浏览器中仅显示该部分。不是我试图运行的实际 PHP 东西。有任何想法吗?

I also tried it with

我也试过

<html>
<body>

<h>Php File</h>

<form action="helloworld.php" method="post"></form>

<p>Did it work?</p>

</body>
</html>

As an HTML file, and then running the following PHP script in a file called helloworld.php

作为 HTML 文件,然后在名为的文件中运行以下 PHP 脚本 helloworld.php

<html>
<body>

<?php
echo "hello world";
?>

</body>
</html>  

I cannot figure out why neither is working. Please help me get past this easy part so I can get to the hard stuff!

我无法弄清楚为什么两者都不起作用。请帮助我克服这个简单的部分,这样我才能解决困难的问题!

回答by John Conde

You can't just open the file in your web browser. You need to run it in a web server. Depending on your platform there is

您不能只在 Web 浏览器中打开文件。您需要在 Web 服务器中运行它。根据您的平台,有

回答by wallyk

PHP is not intended for execution in a browser. It is for web servers to execute, or other preprocessing on the PHP-installed computer.

PHP 不适合在浏览器中执行。它用于在安装了 PHP 的计算机上执行 Web 服务器或其他预处理。



PHP runs in several incarnations when installed on a computer:

PHP 安装在计算机上时会以多种形式运行:

  • from the command line
  • from a web server
  • spawned internally by an IDE or GUI frontend for PHP
  • 从命令行
  • 从网络服务器
  • 由 PHP 的 IDE 或 GUI 前端在内部生成

The web server use is common. A browser asks a web server to retrieve somepage.html. A webserver (like Apache, IIS, etc.) retrieves somepage.html and preprocesses it. If a PHP tag is detected, it calls the installed PHP system to parse the PHP tag. Any result is substituted into the web page being rendered. The web server repeats this process for all tags needing preprocessing, then delivers the rendered page over the network wire to the browser.

Web 服务器的使用很常见。浏览器要求 Web 服务器检索 somepage.html。网络服务器(如 Apache、IIS 等)检索 somepage.html 并对其进行预处理。如果检测到 PHP 标签,它会调用已安装的 PHP 系统来解析 PHP 标签。任何结果都将替换到正在呈现的网页中。Web 服务器对所有需要预处理的标签重复此过程,然后通过网络线路将呈现的页面传送到浏览器。

回答by Francesco Belladonna

PHP is a server-side language (the browser will not create problems to it and won'trun it, because doesn't know anything about PHP!!!). The important thing that you must understand is that PHP produce HTML, it's not an HTML extension, it's something that allows you to generate an html page.

PHP 是一种服务器端语言(浏览器不会给它带来问题,也不会运行它,因为对 PHP 一无所知!!!)。您必须了解的重要一点是 PHP生成 HTML,它不是 HTML 扩展,它允许您生成 html 页面。

That being said, your code is not working because you need the PHP interpreter to parse your PHP code and then give you the html page generated through this code.

话虽如此,您的代码无法正常工作,因为您需要 PHP 解释器来解析您的 PHP 代码,然后为您提供通过此代码生成的 html 页面。

As someone already stated, wamp and xampp are good points where you want start from to parse your php code and render it over a web page.

正如有人已经说过的,wamp 和 xampp 是您想要开始解析 php 代码并在网页上呈现它的好点。

回答by Maddy

There can be a lot of reasons why PHP is not being processed. But first browser does not execute PHP. It is a server side script and its your server's business. All the browser does is parse the HTML that it is being handed.

PHP 未被处理的原因可能有很多。但是第一个浏览器不执行 PHP。它是一个服务器端脚本,它是您服务器的业务。浏览器所做的就是解析它正在处理的 HTML。

So possible reasons could be:

所以可能的原因可能是:

  1. PHP is not installed properly on your system or the server is not properly installed.PHP module isn't loaded in your apache.
  2. You did not put your scripts in the right place. Put your files in /XAMPP/htdocs and then point your browser to: http://localhost/my_php_script.php
  3. Extension is of your file not PHP.
  1. PHP 未在您的系统上正确安装或服务器未正确安装。您的 apache 中未加载 PHP 模块。
  2. 您没有将脚本放在正确的位置。将您的文件放在 /XAMPP/htdocs 中,然后将您的浏览器指向:http://localhost/my_php_script.php
  3. 扩展名是您的文件而不是 PHP。

If you have done everything above correctly then check your server error log. There is little browser can do when it comes to php. You need to check your server side for the php. After you try above look at your php error log. Check for your phpinfoand search for error_logand set the display_errorsto on. It can help you with what might have caused the error.

如果您已正确完成上述所有操作,请检查您的服务器错误日志。当涉及到 php 时,浏览器几乎无能为力。您需要检查服务器端的 php.ini 文件。尝试以上操作后,请查看您的 php 错误日志。检查您的phpinfo并搜索error_log并将其设置display_errorson. 它可以帮助您解决可能导致错误的原因。

I made all the above mistakes. SO I am assuming you might have done the same. ;)

我犯了以上所有错误。所以我假设你可能也做了同样的事情。;)

I hope it helps.

我希望它有帮助。

回答by nook

What is the extension of your file? And when you view the source of the html file, is the php still there? The code needs to be read by the pre-processor, and you need a webserver to do that.

你的文件扩展名是什么?而当你查看html文件的源码时,php还在吗?代码需要由预处理器读取,您需要一个网络服务器来执行此操作。