edorahg
edorahg

Reputation: 399

Simple PHP echo code not working

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.

Upvotes: 31

Views: 125250

Answers (9)

Atul Raj Gyawali
Atul Raj Gyawali

Reputation: 11

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.

Upvotes: 1

Rahul Soshte
Rahul Soshte

Reputation: 1078

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.

Upvotes: 0

Zaki
Zaki

Reputation: 11

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".

Upvotes: 1

Bala vallivel
Bala vallivel

Reputation: 11

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

Upvotes: 0

Lucio Mollinedo
Lucio Mollinedo

Reputation: 2444

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.

Upvotes: 10

matthew n
matthew n

Reputation: 19

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.

Upvotes: 0

Gunnar Bernstein
Gunnar Bernstein

Reputation: 6222

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.

Upvotes: 2

Kirk
Kirk

Reputation: 16265

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.

Upvotes: 34

Rene Pot
Rene Pot

Reputation: 24815

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?

Upvotes: 23

Related Questions