Wells-it.com - Web Hosting

Can anybody tell me how to add PHP code into a HTML file?

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 12 posts

Joined: Jan 2007

Can anybody tell me how to add PHP code into a HTML file?
I used codes for publishing, but it did not work.

Greg K's picture
Moderator

He has: 1,667 posts

Joined: Nov 2003

Most of the time, you can't. You must first change the HTML file over to a PHP file by changing it's extention from .html to .php Then to add the php code, you just wrap it in PHP tags within your original code. PHP tags are either <? ?> or

<?php
 
?>

depending on server settings.

Example...

Original file (testfile.html)

<html><head><title>Test Page</title></head>
<body>
<h2>Welcome to my test page</h2>
</body>
</html>

Add Php to it and rename it (testfile.php)

<html><head><title>Test Page</title></head>
<body>
<h2>Welcome to my test page</h2>
<p>We have detected you are accessing this page from
   <?
      echo $_SERVER['REMOTE_ADDR'];
   ?>
</p>  
</body>
</html>

Some notes:

1. Technically it is possible to use the orignal .html file for PHP code if your server is set up to do so. Most are not though.

2. For the example I used, just sending a variable to the output, you can use the following shortcut instead

<?php
= echo $_SERVER['REMOTE_ADDR']
?>

-Greg

[This space intentionally left blank]

Cool Geek Supplies: www.ThinkGeek.com

They have: 12 posts

Joined: Jan 2007

Thanks Greg, I will try your method Monday.

They have: 12 posts

Joined: Jan 2007

I tried your code, it indeed works, (shows my IP address), but after I copied Ads codes I got from other website, I got:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in line 39,

line39:
".$arr_xml['Text'][$i]." ".$arr_xml['AfterText'][$i]."

\n";

do I need to modify .htaccess file?

by the way, if I delete .htaccess file, I can not open my page, always ask "............... save file?",
can anybody tell me why, there was not .htaccess file several weeks ago, my website even runed good.

Busy's picture
Modrater

He has: 6,157 posts

Joined: May 2001

Whats on line 37 and 38 ?

the line should either start with echo (or print) or be assigned to a variable: $adcode = "....

<?bhb if(broken){ echo("It wasn't me Smiling "); } ?>
Learn HTML the ez way - EzHTML.net

Some people are like slinkies, they dont really serve any purpose but they still bring a smile to your face when you push them down the stairs ...

They have: 12 posts

Joined: Jan 2007

Acturelly, I think the codes are correct, I saved codes as PHP file, uploaded to server, they did work, but if I insert codes into HTML file, they do not work. I think maybe server does support it.

Line 35 to Line 42
if ( is_array($arr_xml) ) {
echo "\n

    n";
    for ($i = 0; $i < count($arr_xml['URL']); $i++) {
    echo "
  • ".$arr_xml['BeforeText'][$i]."
    ".$arr_xml['Text'][$i]." ".$arr_xml['AfterText'][$i]."
  • \n";
    }
    echo "

";
}