Jump to content

Problem with ECHO


Shipton

Recommended Posts

I have no idea what the hell is going on, got me baffled up the wazoo.

 

I'm just learning php, and I'm doing your run-of-the-mill Hello World! thing-a-ma-jigger. Code below. However, when I run it, nothing shows up at all. Blank page. Funny part is, I have another file called phptest.php, which makes it spew out all the relevant information about php to verify it's working, which it apparently is, because that one line of code works.

 

I have a virtual host set up on my computer via Apache, with PHP and MySQL installed. The file below is called index.html. I am accessing it via localhost, not via C:\whatever, so that's not the issue. Any help is greatly appreciated.

 

<html>

<head>

<title>Main Index</title>

</head>

<body>

<?php

echo "Hello world!"

?>

</body>

</html>

 

I changed it to .php, and thus it works. I'm a class-A idiot. But does it have to be .php, or can an HTML document ever contain PHP, much as it can contain javascript?

Link to comment
Share on other sites

Yes, it can work with a .html extension (or, indeed, any other extension you like). It's an apache setting. Ill just try and find it for you.

 

Edit: Find this line in your http.conf: AddType application/x-httpd-php .php

Then add this line:  AddType application/x-httpd-php .html

 

On a side note, php wont throw an error for a missing semi-colon if the php is closed directly after.

Link to comment
Share on other sites

Getting into the habit of leaving off the last semi-colon before a closing ?> tag will cause you problems when you add lines of code after that line and forget that you need to go back and add the missing semi-colon. It is best to not take lazy-way short cuts when writing code.

Link to comment
Share on other sites

<?php

echo 'Hello world!';

?>

 

Use '' for strings with no variables, and "" for variables included in the echo. (DONT QUOTE ME ON ITTTT) Also for like

<?php

$fun='James';

$fun2="$fun + Bond";

echo "$fun2"

?>

 

that would say James Bond. I think. I'm so tired and have had no coffee. So you pros, dont bother telling me it isn't correct because it might not be.

Link to comment
Share on other sites

Well you learn something new every day

 

I was thinking the same thing in that I didn't know there was a way to serve up php on html pages. I thought they had to be php.

 

What I wonder though, is do $_POST variables still work on these pages? I know that usually trying to $_POST to an html page will cause an error, but does that error not happen when using this method?

Link to comment
Share on other sites

When you tell Apache to process a file via PHP, it doesn't matter what the file extension is.

 

You could easily say

AddType application/x-httpd-php .xyz

 

And your apache webserver would process "yourfile.xyz" with the PHP processor.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.