0

Good afternoon,

So I met a problem, I can't display PHP file results in HTML.

As example I have php file named hi.php that contains code like this:

<?PHP
$hi="Hello!";
echo "$hi";
?>

And I have index.html that contains body like this:

<!DOCTYPE html>
<html>
<body>
<?php include "taskai.php" ?>
</body>
</html>

And I get a blank page... No results displayed from hi.php. I'm working on localhost, xampp. May this be the problem, or I have something wrong?

  • 4
    Good afternoon, Tomas. By default, HTML files doesn't load php commands: try to rename it in `index.php` – fusion3k Mar 08 '16 at 14:57
  • Do you really get a *blank* page? What do you see when you view the page *source* in the web browser? – David Mar 08 '16 at 14:57
  • 1
    as @fusion3k said, if your file ends with `.html` then any PHP in it will not work. However, the `hi.php` code should work by itself. What is your URL like when running `hi.php`? – CodeGodie Mar 08 '16 at 14:59
  • _And I get a blank page_...If it's true then might be something wrong in `php.ini` – Domain Mar 08 '16 at 15:02
  • why are you showing this `taskai.php` in your code? is that relevant? – CodeGodie Mar 08 '16 at 15:03
  • @fusion3k, that worked well. But what should I do to echo result in HTML ? :) –  Mar 08 '16 at 15:03
  • you have to use [`include("taskai.php")`](http://php.net/manual/en/function.include.php) instead of [`show_source("taskai.php")`](http://php.net/manual/en/function.show-source.php) – fusion3k Mar 08 '16 at 15:04
  • @david, in code I get my query as a comment `` @CodeGodie, the point is to show something from php file in HTML file :) I will use `include filename` because I need to display the result only –  Mar 08 '16 at 15:05
  • 1
    you need to instruct your system to treat `.html` files as PHP if you haven't already. If not, then give it the `.php` extension. You running this off a webserver or not? – Funk Forty Niner Mar 08 '16 at 15:05
  • @TomasVilemaitis: Then clearly the PHP code isn't being evaluated. Which by default doesn't happen in `.html` files. You can rename it to `.php`, or you can configure your server to process `.html` files with PHP. (The former option is preferred, as the latter creates unnecessary overhead for HTML files.) – David Mar 08 '16 at 15:06
  • 2
    and if local, you accessing as `http://localhost/file.xxx` or as `file:///file.xxx`? Two different animals here. – Funk Forty Niner Mar 08 '16 at 15:06
  • Thank you for your time and answers guys! Solution is to change `index.html` to `index.php` or move to webserver :-)) Thank you once again! –  Mar 08 '16 at 15:10

2 Answers2

0

Create a file index.php with that content:

<!DOCTYPE html>
<html>
<body>
<?php
$hi="Hello!";
echo "$hi";
?>
</body>
</html>

That should give you an idea of how things work.


There obviously are endless variants. For example you can also do this:

File index.php:

<?php
$hi="Hello!";

echo <<<EOT
<!DOCTYPE html>
<html>
<body>
{$hi}
</body>
</html>
EOT;
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

Two problems:

  • your HTML file should have the .php extension for it to understand PHP code change index.html to index.php

  • you are not using include to include the code onto your HTML

    Do it this way:

    index.php

    <!DOCTYPE html>
    <html>
        <body>
         <?php 
         include "taskai.php" 
         ?>
        </body>
    </html>
    
CodeGodie
  • 12,116
  • 6
  • 37
  • 66