-2

I am trying to do something basic with PHP and HTML first before I get into something big. However, my html page doesn't seem to be processing PHP correctly.

I have the following php code in my html:

<?php echo '<p>Hello World</p>'; ?>

However, the output on the html page is: 'Hello World' on one line but:

'; ?> follows hello world How can I fix this so I get 'hello world'?

hippoman
  • 187
  • 2
  • 10

2 Answers2

0

PHP doesn't run on browsers, it's executed on the server. To work with PHP on your local machine, you'll want to set up a server, and write in HTML and PHP in a file with a .php extension, not a .html one. The easiest way to get started would be to use xampp as a beginner.

Mav
  • 1,087
  • 1
  • 15
  • 37
-1

It looks like you are trying to put php code inside a .htm or .html file.

I ran this code as just html and got the following:

Hello World

'; ?>

So, it is properly interpreting the <p></p> tags but everything else is meaningless to it and doesn't know what to do with what comes after it, so it just prints it as is.

To use php, you need to be doing it inside a .php file and you need to be accessing it from a server that recognises .php files. If you are doing this locally, simply opening the file in a browser won't work by default. You will need to setup a local web server that is running a version of PHP. if you don't have much experience, I recommend WAMP because it is easy to set up and run php with.

If you are doing this on a website that is actually hosted (not local), most of them have support by default for PHP, so if you are using a .php file and it isn't working, you should contact the host or read their documentation to figure out how to get their servers to interpret php files.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Alex Walker
  • 36
  • 1
  • 5