1

I am working on an internet programming project and this is my first assignment in PHP. I read that you cannot put PHP code inside of HTML but my professor has these instructions which contradicts that. instrcutions-part 1 instructions-part 2 Can someone please interpret what he might mean? Thank you in advance!

  • If you have questions like this on an assignment, why don't you ask the person who wrote it/gave it out? You are allowed to do that. – TimBrownlaw Jun 24 '19 at 02:10
  • You would need to modify your `.htaccess` to allow PHP to run inside `.html` files, but yes, you can do so. – Obsidian Age Jun 24 '19 at 02:13
  • This professor is extremely strict. He will sometimes deduct 10% for asking questions. And that’s why I’m afraid to also change the extension, because it needs to be exactly to his specifications. But if that’s the only way then I guess I’ll do that – mastercooler6 Jun 24 '19 at 02:15
  • 4
    " He will sometimes deduct 10% for asking questions" that's utterly ridiculous and unacceptable in any university –  Jun 24 '19 at 02:24
  • 2
    You should be getting Brownie Points ( extra points) for asking any question. That is what he is there for - to teach and answer questions... It's Bloody ridiculous. Anyway have you actually checked that the server you will be using isn't already setup to run php in .html files? That would be your first thing to check. Just make a lil test file to run. – TimBrownlaw Jun 24 '19 at 02:51
  • @TimBrownlaw What kind of test file? I tried with a simple echo statement and it didn’t work – mastercooler6 Jun 24 '19 at 03:04

2 Answers2

1

You can certainly use HTML in PHP if your server is set up for it.

echo '<div>' $message . 'was the value entered. </div>'
Ziggy
  • 43
  • 6
1

In order to run PHP inside .html files you need to send header request for the app to be able to recognize the markup. Otherwise PHP wont run.

To do this you need to create a .htaccess file in your root web directory and add this line to it:

AddType application/x-httpd-php .htm .html

Or you can use this simple rule in your .htaccess :

RewriteRule ^([^.]+)\.html$ $1.php [L]

On a side note, using PHP inside .html files is very unnatural. It is preferred to use extension .php that's what it is meant for. You can naturally use HTML inside PHP. My interpretation from your assignment is that your teacher is asking you to use HTML inside PHP and not the other way around.

You want to use an if statement to check whether a variable is empty or not :

if($message !='' || $message !=null){

echo $message;

}else{

echo 'Variable is empty';

}

Have a look at the php documentation for more PHP statements, what they are and how to use them : https://www.php.net/manual/en/language.control-structures.php