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.

3
  • You need a webserver with PHP support (such as Apache httpd) and not just a browser. Commented Jan 8, 2014 at 17:13
  • Also, make sure your file ends in php, as in somepage.php. Commented Jan 8, 2014 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 Commented Jan 8, 2014 at 17:14

3 Answers 3

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.

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

2 Comments

May I ask what the downvote was for? Is there something incorrect in my answer?
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
0

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

Comments

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)

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.