Let's say I just want to use PHP include to grab HTML from another file. can I just put in that little PHP script and name my .html file (index.html) to index.php and it'll work? I thought I'd have to add my server password an other info in PHP to use it.. What do I do?
6 Answers
No you can't just insert PHP into HTML and expect it to work.
PHP is a server side language that generates the HTML that's sent to the client's web browser. The files usually have the extension ".php" rather than ".html" but simply renaming "html" as "php" won't work.
You need to have a PHP parser installed on your server and reconfigure your whole site.
You might be thinking of JavaScript which can be inserted into HTML and is run client side.
2 Comments
omnix
Okay thanks, and no I wasn't thinking about JS. I'm just really new to PHP. everybody elses answers was great too :). But when do I need to insert server details.. etc?
ChrisF
@Kawohi - you'll need to talk to your service provider about converting a plain html site to a php one. They should have instructions on what you need to do. You might have to move the site to a new server though.
Exactly as you said:
<html>
<head>
<title>Test PHP file</title>
</head>
<body>
<?php
echo 'test';
//all your php code can go in here
?>
</body>
</html>
You can have multiple <?php ?> blocks in your file.
4 Comments
ChrisF
This will work only if the server is configured to parse php files.
silvo
Yeah, that's an initial prerequisite. Thanks for pointing that out.
Alexander
@ChrisF - Most servers are configured for PHP, and if they aren't, they're probably configured for ASP.
silvo
Agreed, the OP might also expect that it will work when loaded from a local hdd, so it needed to be clarified.