0

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 6

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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.
1

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

This will work only if the server is configured to parse php files.
Yeah, that's an initial prerequisite. Thanks for pointing that out.
@ChrisF - Most servers are configured for PHP, and if they aren't, they're probably configured for ASP.
Agreed, the OP might also expect that it will work when loaded from a local hdd, so it needed to be clarified.
0

This will all work if your server is configured to parse HTML with the PHP interpreter. I made an Apache handler including application/x-httpd-php5 .html .htm Then yes. You would be correct. I suggest doing this with an Apache .htaccess or a hosting panel.

Comments

0

If your server is configured to parse php files, then yes, adding <?php include('somefile.html'); ?> should work fine.

Comments

0

no you don't need server password to run php... just rename the file to .php and insert your php code inside <?php ?>

Comments

-2

PHP and HTML

test.php

<?php
define('title','foo');
?>
<!doctype HTML>
    <html>
        <head>
            <title><?=foo?></title>
        </head>
    <body>
        ...
    </body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.