4

I am new to PHP, just downloaded it, and wrote a "Hello, World!" program:

<html>
    <body>

    <?php
        $fileName = "test.jpg";
        $txt = "Hello, World!";
        echo $txt;
    ?>

    </body>
</html>

But it doesn't work. I named the file test.php and opened it with Firefox and nothing was displayed.

4
  • 1
    Did you open through localhost or just from the filesystem (file://) ? Nov 16, 2009 at 2:54
  • Also, what webserver? Apache or IIS or ? Nov 16, 2009 at 2:54
  • Apache works on most OS, and is relatively easy to install. It's also free (not only as in free beer) and works well with PHP
    – Marius
    Nov 16, 2009 at 2:58
  • if you are brand new to php then you might also consider trying some python web development platform or ruby-rails and see which is better for you.
    – Evgeny
    Nov 16, 2009 at 3:05

6 Answers 6

16

You'll have to pass it through a webserver of some kind, for example IIS or Apache, for it to work.

PHP is a server-side language, thus you can't just open the PHP file in a browser. Instead, try googling some free PHP hosts, and upload your files there.

You could for example go for these solutions that will provide you with what you need:

Those solutions will install Apache, MySQL and PHP and allow you to play locally with PHP development. Later on, if you want to deploy your code, you will have to find an external host (or host it on your own computer).

1
  • You are right, I didnt open it with a websever - the short online tutorial didnt say anything about that. Will try WAMP. Thanks a lot guys!
    – LikeToCode
    Nov 16, 2009 at 3:14
6

You need to have a webserver that you open it through. Simply entering file:///whatever.php does not work, because the file is not parsed by PHP then.

There are several web servers you can chose from, but my advice is Apache, the worlds most popular webserver. I have it running on my Windows XP laptop along with PHP, and it's easy to install and setup, and works great. There are lots of modules that can be installed later if you need them. It's also free, not only as in free beer, but also as in open source.

0
5

You will need to install a webserver on your machine to make it work. Take a look at XAMPP at http://www.apachefriends.org/en/index.html.

2

If you have command-line PHP installed, you should be able to enter:

php test.php

to invoke PHP to run your program and display the output on the console.

2

You need a webserver to view the PHP program like XAMPP or WAMP. If you are using XAMPP, put your file in htdocs and then access your file from any browser like Google Chorme, Firefox or Internet Explorer by typing localhost/foldername/yourfile.php, in your case localhost/test.php.

1

Do you have a local server installed? PHP is a server-side scripting language, so it cannot just be run like a normal HTML file.

Try watching the first few videos on the New Boston's PHP series. They show you simply how to download, install and use a local server to run your PHP on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.