1

I'm sure this is simple and stupid on my part, but my php code keeps showing up in my html page.

This is just a simple test:

<html>
<body>
Test 1<br>
<?php

print "Test 2";
echo "Test 3";
print "<b>Test 4</b>";

?>

Test 5<br>

</body>
</html>

And this is how it looks:

Test 1<br>
Test 4"; ?> Test 5

What the heck am I doing wrong? Above I've mixed echo and print to show it doesn't seem to make a difference.

Marc B
  • 356,200
  • 43
  • 426
  • 500
AdamP_OH
  • 55
  • 1
  • 1
  • 6
  • Have you looked at the HTML source of the page? I think it's because your web server isn't configured properly, and isn't parsing PHP before sending it. – andrewsi Sep 10 '12 at 20:46
  • What's the file name of the page you're trying to view? Can you create a page named test.php with the content of `` and run that? – j08691 Sep 10 '12 at 20:49
  • Do a `view source` of that page, you'll most likely see your PHP code embedded within, where the browser is interpreting the ` – Marc B Sep 10 '12 at 20:52
  • Have a look at http://www.youtube.com/watch?v=E8QZk7owFZ4&feature=edu&list=PL960338B143E7F889 – Abhishek Gupta Sep 10 '12 at 20:53
  • When I do view source I see all of my php code. My web server is configured for php, we have many php pages that display properly. It is only this one test I'm trying to do with php in an html document that shows up strangely. – AdamP_OH Sep 11 '12 at 12:44

2 Answers2

10

You are not accessing the program through a web server that supports PHP and is configured to parse that page for PHP directives.

  • You must access the page through an HTTP server (e.g. Apache HTTPD), not directly from your file system.
  • The HTTP server must have PHP installed
  • The page must match the configuration for PHP files (by default this is normally "Has a .php file extension")
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

if you are using a local server (XAMPP, for example), and you access your webpage by double clicking on the html file, then the address in the browser will be

file:///C:/xampp/htdocs/(filename).html

which will NOT run PHP for some reason. The correct address to use to access your html file with PHP code in it is

http://localhost/(filename).html

which accesses the html file in a way that the PHP works, and needs to be entered directly into the browser, then bookmark your local host directory so you can easily access your files through the browser instead of clicking on the icons in explorer or through the terminal

fyi the address prefix is persistent, so it will carry through to linked pages, which means if PHP doesn't work on the first page then it won't work on any of the other pages on your website, conversely, if the first page works, then the rest will too

insomnia
  • 11
  • 1