1

I am just starting to learn PHP and I don't quite understand why it doesn't work.

I am trying a simple echo statement but nothing shows up when I open it in Chrome.

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <p>
      <?php
        echo "hello";
      ?>
    </p>
  </body>
</html>

Is this not how you test out PHP? Really confused, any help is appreciated.

user2397282
  • 3,798
  • 15
  • 48
  • 94
  • You need a webserver with PHP support (such as Apache httpd) and not just a browser. – Tobias Jan 08 '14 at 17:13
  • Also, make sure your file ends in php, as in somepage.php. – Robbert Jan 08 '14 at 17:13
  • Followed with @still_learning, Your file extension should be `.php` and should be executed/run in browser like `http://localhost/yourfoldername/yourfilename.php` – Krish R Jan 08 '14 at 17:14

3 Answers3

2

It sounds like your file isn't being parsed as PHP. I bet if you look in the source, <?php echo "hello"; ?> will be there, in plain text. Make sure your host supports PHP, and name your file appropriately to get it to be parsed as PHP (eg, with .php extension).

If you're testing this locally, you can use PHP's built-in web server. From command line (replacing ~/public_html with the path to your code):

cd ~/public_html
php -S localhost:8000

Then, open http://localhost:8000 in your browser.

0b10011
  • 18,397
  • 4
  • 65
  • 86
  • May I ask what the downvote was for? Is there something incorrect in my answer? – 0b10011 Jan 08 '14 at 19:50
  • Due to stackoverflow being evil.... I down voted the answer before you included example code, But I cant remove it because I have made to many up votes in the last hour :P – Burdock Jan 08 '14 at 20:24
0

The issue is PHP is a server side language, you need a functioning server to interpret the code and (in this case) output the appropriate HTML and CSS ~

Read more about PHP as a server side language here

If you want to test your code locally you will need to use:

$ /path/to/php myfile.php // unix
C:\php\php.exe myfile.php // windows

You can read more about local web servers here

It's worth adding that PHP from version 5.4 onwards is able to run a web server on its own. You can do it by running this code in a folder which you want to serve the pages from:

$ php -S localhost:8000

(Credit where credit is due)

Community
  • 1
  • 1
Burdock
  • 1,085
  • 1
  • 9
  • 22
0

Save the file with php extension and open it with the help of a web server(apache)

Praveen
  • 733
  • 2
  • 7
  • 16