31

Here is my html with a php script:

  <html>
      <head>
    <title>Bob's Auto Parts</title>
  </head>
  <body>
    <h1>Bob's Auto Parts</h1>
     <table width = 100% >
         <tr>
             <?php
                 echo "<td>This is working.</td>";
             ?>
         </tr>
     </table>
  </body>
 </html>

Why is the output of this appearing with a ; ?>. I want it to be 'This is working.' only. Here is the ouput

Bob's Auto Parts

Bob's Auto Parts

This is working."; ?>

I know I am doing something wrong here but not able to figure it out. Thanks in advance.

edorahg
  • 399
  • 1
  • 4
  • 11

9 Answers9

34

Make sure that you are using <?php and not <? shorthand since that may be disabled on your server. This will cause the output of "; ?> as it happened to me a few months ago in a transition to PHP5.

I've only seen the odd output like this when the PHP parser isn't detecting it as PHP. Make sure to check that PHP is functioning as expected and that the <?php tag is being recognized.

Kirk
  • 16,182
  • 20
  • 80
  • 112
23

Any of these (or more) could be your answer why it is not working

  1. Is there actually PHP running on your computer?
  2. Is the file extension .php?
  3. Are you accesing the file through your browser doing something like http://localhost/myfile.php
  4. If it is on a remote server, is there PHP installed?
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
  • - Yes. PHP is running on my computer. I have run other PHP file successfully. – edorahg Oct 03 '11 at 18:59
  • - The file extension was php. But that did not work. So i renamed it to html and then tried. It worked but with this output. – edorahg Oct 03 '11 at 18:59
  • 1
    you should put .php back, .html is not for php – Rene Pot Oct 03 '11 at 19:02
  • Thanks. That made it work. Not sure why it was not working earlier when it was .php. At that point Firefox was bringing up the open/save dialog for the .php file. I will now try to implement the full script and see how it behaves. Thanks for the fix though. – edorahg Oct 03 '11 at 19:08
  • 2
    How would one check id php was running? Would it show up on task manager? Also, why does it matter if the file is accessed like that? – ApproachingDarknessFish Nov 24 '12 at 22:59
  • I have done all the points you mentions but still I am having the same error. @Rene Pot – alper May 16 '18 at 18:04
10

In my case (which is a very specific case) installing this missing package (Ubuntu 14.04) did the trick:

sudo apt-get install libapache2-mod-php5

for users working with php7 install the package:

sudo apt-get install libapache2-mod-php7.0

After that, just restart apache:

sudo service apache2 restart

And there you go.

bugHead
  • 113
  • 1
  • 8
Lucio Mollinedo
  • 2,295
  • 1
  • 33
  • 28
  • 1
    Similarly, going from 14.04 to 16.04, they changed the version of PHP to 7 and I had to install it by hand! – Alexis Wilke Jun 01 '16 at 01:31
  • For Linux Mint Cinnamon (serena) edition 18.0, second step install libapache2-mod-php7.0 will fix the issue. Thanks for the post. – Xan May 18 '17 at 21:29
2

I had the same problem when I figured out my mistake:

Instead of correct http://localhost/test.php I just double clicked on the file file:///C:/Users/.../htdocs/test.php.

Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
1

The file was being saved in UniCode Encoding. Opened the file in Notepad, and saved by changing the Encoding to "ANSI" and saved the file as fileName.php and type "All Files", Encoding as "ANSI".

Zaki
  • 11
  • 1
1

Make sure the extension is .php and check echo tags on phpmanual. php.net/manual/en/function.echo.php

When you try to include html tags inside echo function, you need to insert between "" or '' and use . between elements.

0

In order to display PHP, or run embededded PHP, you need to tell PHP to look inside the HTM & HTML files.

You need to have a file in the root folder of the HTML directory called .htaccess, which is a simple text file, with the following line:

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

This lets the PHP compiler know to compile the php when displaying the HTML page.

Tim Malone
  • 3,364
  • 5
  • 37
  • 50
0

http://localhost/demo/demo.html will not work. http://localhost/demo/demo.php will work. php work on .php extension. Good Luck bro

Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
0

Check if you have installed more than 2 versions of PHP. The server may get confused sometime regarding this. First uninstall the php versions and reinstall only one PHP version.

Rahul Soshte
  • 798
  • 8
  • 11