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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Solution 6

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply