LikeToCode
LikeToCode

Reputation: 767

"Hello, World" in PHP displays nothing

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.

Upvotes: 4

Views: 853

Answers (6)

Sam
Sam

Reputation: 359

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.

Upvotes: 1

trojan x
trojan x

Reputation: 21

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.

Upvotes: 2

GreenMatt
GreenMatt

Reputation: 18590

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.

Upvotes: 2

hoball
hoball

Reputation: 3216

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.

Upvotes: 5

fiskah
fiskah

Reputation: 5902

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).

Upvotes: 16

Marius
Marius

Reputation: 58931

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.

Upvotes: 6

Related Questions