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

They have: 13 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

He has: 2,145 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

They have: 13 posts

Joined: Jan 2007

Thanks Greg, I will try your method Monday.

They have: 13 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.

[URL="webmaster.helpisland.com"][COLOR="Blue"][B]Webmaster Tools[/B][/COLOR][/URL] | [URL="baike999.com"][COLOR="Red"][B]Work At Home[/B][/COLOR][/URL]

Busy's picture

He has: 6,151 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 = "....

They have: 13 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 "

";
}

[URL="webmaster.helpisland.com"][COLOR="Blue"][B]Webmaster Tools[/B][/COLOR][/URL] | [URL="baike999.com"][COLOR="Red"][B]Work At Home[/B][/COLOR][/URL]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.