0

I am trying to implement PHP code onto a html page, something I've done before without any problems. Now, however, the PHP flag closes prematurely and so PHP code is printed to the page instead of run. My page is similar to this:

<html>
<body>
    <h1>Test</h1>

    <?php
        $array = array(
            "foo" => "bar",
            "bar" => "foo",
        );

        echo($array["foo"]);
    ?>
</body>

I would expect the page to then return;


Test

bar


However, the PHP stops at the first instance of '>' so leaves;


Test

"bar","bar"=>"foo",);echo($array["foo"]);?>


Is this a problem with the apache configuration? How can I fix it?

Gregory Sims
  • 511
  • 7
  • 18
  • 4
    Does your file have a .php extension? If not, Apache does not know to hand it to PHP to be processed. if you view the page source, you'll in fact see _all_ your code. Part of it is just misintrepreted as broken HTML, and that's the reason for the strange output you got. – Michael Berkowski Oct 23 '14 at 01:23
  • If that is indeed the case, [this question](http://stackoverflow.com/questions/2198680/php-script-not-working-in-html-file) has a solution for telling Apache to send .html files to the PHP interpreter. If this isn't your issue, please let me know here in the comments and I'll reopen the question. – Michael Berkowski Oct 23 '14 at 01:27
  • your code works fine. I actually copied it and tested it. Maybe apache isn't configured properly? – just some guy Oct 23 '14 at 01:30
  • It does work (http://codepad.org/aZdODips); the clue lies in that fact you're staring at raw PHP in the page's rendering. You're lucky in the sense that usually you *don't* see raw code in the rendered output and it takes half an hour to think to view source. Tada! – Jared Farrish Oct 23 '14 at 01:31

0 Answers0