0

So I am developing a webpage and I am starting to dig into PHP. I went to WS3 schools and looked at the example. I copied it over to an html document. When I double clicked on it and it opened in chrome the PHP would not show up.

Here is the example code:

    <!DOCTYPE html>
    <html>
<body>

    <?php
    echo "My first PHP script!";
    ?>

</body>

8
  • Have you closed the html tags? Commented May 19, 2013 at 18:41
  • are you accessing your file through a server with php support? Commented May 19, 2013 at 18:41
  • 2
    your file needs a .php extension. (Instead of .html) Commented May 19, 2013 at 18:41
  • 1
    You need a PHP server to render the php tags. It would not work out of the box Commented May 19, 2013 at 18:41
  • 4
    w3schools is a wrong and misleading site. You shouldn't use it as reference for any sort of language. For PHP, there's the PHP Manual, for JavaScript, there's Mozilla Developer Network (or MDN). See w3fools.com to further understand why you should never use w3schools. Commented May 19, 2013 at 18:42

4 Answers 4

3

You need a functioning web server that can run PHP in order to be able to parse PHP files.

Just writing PHP and opening the resulting HTML file in the browser won't do.

Have a look at quick local environment such as XAMP or WAMP.

Sign up to request clarification or add additional context in comments.

5 Comments

since PHP 5.4 you don't need to install apache and struggle with its configuration, you can just run php -S localhost:8080 in webroot directory of project
@lupatus: That's still not 100% recommended, mainly because you want your local environment to be closer to the one your live server would probably eventually be using.
I agree that for real project its not the best way, but for 'hello world' its enough ;)
@lupatus: Sure, but I wouldn't want him to get too comfortable with that workflow. Even setting up a basic server using XAMP is considered "bad practice", the ideal would be to manually insteall Apache/Nginx, PHP, MySQL, and configure everything properly.
I just set up WAMP and that seems to run fine. Thanks guys.
0

PHP cannot be directly rendered by the browser. It needs to be interpreted by a server first. For eg: apache.

If you are on windows using something like WAMP, as suggested by Madara, would help

If you are on Linux, install apache

Comments

0

To learn php you need to set up local server:

  1. Install XAMMP (http://www.apachefriends.org/en/xampp-windows.html)
  2. Thne run Apache Server
  3. Put script in htdocs
  4. Point your browser to http : // localhost

Or just use online service like http://ideone.com/

Comments

0

Type in the the terminal/console when your navigated into your scripts directory:

php -S localhost:8080

And then go to http://localhost:8080 in your browser

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.