omnix
omnix

Reputation: 1899

New to PHP, can I just insert PHP in code?

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?

Upvotes: 0

Views: 141

Answers (6)

Jet
Jet

Reputation: 1325

PHP and HTML

test.php

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

Upvotes: -2

eleete
eleete

Reputation: 56

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.

Upvotes: 0

ChrisF
ChrisF

Reputation: 137148

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.

Upvotes: 6

Atul Dravid
Atul Dravid

Reputation: 1065

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

Upvotes: 0

silvo
silvo

Reputation: 4009

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.

Upvotes: 1

Mchl
Mchl

Reputation: 62387

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

Upvotes: 0

Related Questions