Need help with PHP snippet in HTML file?

Question by Guy: Need help with PHP snippet in HTML file?
I’m trying to put the following code into an HTML file (it must be html for reasons too long to explain). But unfortunately when it hits the > in my code it thinks my php snipped has ended and displays the rest as html.

$ item_array[] = array(
'title' => $ title,
‘url’ => $ url,
‘description’ => $ description,
‘timestamp’ => $ timestamp
);
?>

Any ideas how I can parse that => so it still does it’s job in the php snippet without having the HTML think it is an end tag?

Best answer:

Answer by David
if you use the HTML extension, your web service will not know that its php, so it will not compile it.

I tried just echoing “Hello World” but the browser and my web server have no idea how to process this.

Now, if you use php to run the html, it will work just fine, so I dont see a way to do what you want without invoking php.

Add your own answer in the comments!

No related content found.

Revisions

There are no revisions for this post.

Tags: , , , ,

3 Responses to “Need help with PHP snippet in HTML file?”

  1. apple January 19, 2011 at 5:06 AM #

    You need to save it as a PHP file. Otherwise the web server will interpret it as HTML and send it as HTML. You can fix this however:
    Use mod_rewrite, if you are using apache. However, this may not work for you reasons as underneath it IS still a php file, only disguised as HTML to the visitor.
    You can configure some server environments to execute HTML files as scripts, but be careful, you may accidentally create security issues or execute code you didn’t mean to. Contact your web host (Godaddy can do this, but there may be a way to configure apache too)

  2. just "JR" January 19, 2011 at 5:13 AM #

    Three possible reasons:
    - You did not save your file with the entension “.php” (A file with extension .html will NOT be Php parsed)
    - You do not have Php enabled on your server.
    - Your code does not do anything but create an array you cannot use in HTML. (since $ title, $ url, $ description and $ timestamp have not been defined).

  3. cristianods January 19, 2011 at 5:21 AM #

    Do a small change in your code.

    < ?php
    $ info = array();
    $ info['title'] = $ title;
    $ info['url'] = $ url;
    $ info['description'] = $ description;
    $ info['timestamp'] = $ timestamp;

    $ item_array[] = $ info;
    ?>

    If this doesn’t work, check your log files for any abnormal error that might be killing your PHP script.

Leave a Reply