views:

57

answers:

5

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?

A: 

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

Chad
And that the web server is configured to actually run it through PHP. If he's on a web host, that *should* be done for him, but if he's setting up a server at home it might not be.
ceejayoz
The file does have a .php extension and I'm on a web server so I don't think either of those are the problem. Thanks for trying though.
APShredder
A: 

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.

Aaron Harun
Viewing the source shows the original code I posted above with `html` tags included.
APShredder
Then make sure .php files are set to run with PHP.
Aaron Harun
Try adding each of these (one at a time) to a .htaccess `AddType x-mapp-php5 .phpAddHandler application/x-httpd-php5 .phpAddHandler cgi-php5 .php` See: http://forum.joomla.org/viewtopic.php?p=501583
Aaron Harun
+3  A: 

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

Chris T
I am using an FTP server from my local ISP. When I view the source, I can see the code in the php tags, so I guess that means PHP isn't enabled. Is there something
APShredder
A: 

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.

Rachel
A: 

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.

jromero
I've already checked that the file has a .php extension several times, so I doubt that that is the problem. Thanks for trying though.
APShredder