8

Possible Duplicate:
PHP script not running on browser

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:

<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?

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>
<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!

payne
  • 4,691
  • 8
  • 37
  • 85
user1459268
  • 217
  • 1
  • 3
  • 9

5 Answers5

11

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
6

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 runs in several incarnations when installed on a computer:

  • from the command line
  • from a web server
  • spawned internally by an IDE or GUI frontend for PHP

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.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Can you please break this down for me a bit more? I don't quite understand and am eager to get past this point. I have an IDE and believe I have PHP running on my computer. I was under the impression then that after writing php, which can be used/called in html all i would have to do to see results was to open the html file in my browser – user1459268 Jun 21 '12 at 18:50
  • Install wamp or xamp and then place the file in your root folder. To view the file you would then go to localhost\filename.php to view it. – Drewdin Jun 21 '12 at 18:54
  • @user1459268: I have amended my answer. – wallyk Jun 21 '12 at 18:56
5

PHP is a server-side language (the browser will not create problems to it and won't run 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.

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.

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.

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
2

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.

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.

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 phpinfo and search for error_log and set the display_errors to on. It can help you with what might have caused the error.

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

I hope it helps.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Maddy
  • 1,233
  • 2
  • 12
  • 20
-1

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.

nook
  • 2,378
  • 5
  • 34
  • 54