Adam P
Adam P

Reputation: 4713

Echo command doesn't do anything

I've been started studying PHP in my spare time, and the first code example I was given was this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        <?php
        echo "Hello World";
        ?>
    </body>
</html>

From what I understand, this should write out "Hello World". However, all I see is a blank webpage. Any ideas why this is and how I should go about fixing it?

Upvotes: 10

Views: 22503

Answers (5)

romeroqj
romeroqj

Reputation: 839

The code seems fine, certainly it should do what you intend.

Probably what happened is that you named the file with something like example.html, so you have to check the extension. It must look like example.php. With the extension .php at the end of the file you are telling the webserver that this file contains php code in it. That way the <?php echo "Hello World"; ?> is going to be interpreted and do you intend it to do.

Upvotes: 1

Rachel
Rachel

Reputation: 103617

You might want to enable your error reporting in .htacess file in public_html folder and try to diagnose the issue depending upon the error message.

Upvotes: 1

Christopher Tarquini
Christopher Tarquini

Reputation: 11342

Here's a checklist

  • What server are you running? Does it support php?
  • Is PHP enabled?
  • Is your file named with the extension .php?
  • When you use View Source can you see the code in the php tags? If so PHP is not enabled

As a test try saving this as info.php

<?php
phpinfo();
?>

and see if it displays information about your server

Upvotes: 13

Aaron Butacov
Aaron Butacov

Reputation: 34397

If you don't see the html tags in the source, it means there is a PHP error. Check your view source, and if nothing is shown, check your error logs.

Upvotes: 0

Chad
Chad

Reputation: 3237

Make sure the file that contains that code is a PHP file - ends in '.php'.

Upvotes: 1

Related Questions