PHP within a string of HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

PHP within a string of HTML

Post by The Merg »

I have a PHP page that is used to create a web page. The PHP code has mutiple strings that are concatenated together and then printed to create the web page. The issue I have is that I cannot include any PHP within that string. I know I am missing something, but don't know what. Any help would be appreciated.

My string in PHP looks as follows (25 lines):

Code: Select all

 
$htmltop = <<<TopOfPage
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- #BeginTemplate "../styles/integral.dwt" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<!-- #BeginEditable "scripts" -->
<?php
    if(!empty($_GET['OrigRef'])) {
    $orig_referer = urldecode($_GET['OrigRef']);
    print <<<EndOfWeRemembered
        <meta http-equiv="Refresh" content="8;URL=$orig_referer" />
EndOfWeRemembered;
    }
?>
<!-- #EndEditable -->
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<link href="../styles/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
TopOfPage;
 
If I remove the PHP code in the <!-- #BeginEditable "scripts" --> section, the page works fine. I know it has to do with the closing PHP tag as it then thinks the code is done, but I cannot figure out how to get it to think that the PHP tags in the string are just a part of the string.

Also, all 25 lines of my code are there, but you have to scroll down within the code window.

Thanks,
Merg
Last edited by The Merg on Sat Nov 08, 2008 10:36 am, edited 2 times in total.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: PHP within a string of HTML

Post by Syntac »

You can't embed PHP code in a PHP string (without using eval(), which you probably shouldn't). Try this:

Code: Select all

echo htmlspecialchars( file_get_contents( "a_php_file.php" ) );
...and you'll see what I mean.
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Re: PHP within a string of HTML

Post by The Merg »

Okay. I put the code in a separate file and then call for it when "printing" the web page. However, it now just places the PHP code in as text within the web page. My code is as follows:

Code: Select all

 
function show_errors($htmltop, $htmltop2, $errors, $htmlbottom) {
    print $htmltop;
    echo htmlspecialchars( file_get_contents( "../scripts/refreshpage.php" ) );
    print $htmltop2;
    print("<p>There were problems with your submission. Please go
       back to the previous page and correct the following errors:
       </p>\n");
    show_error_list($errors);
    print $htmlbottom;
    
}
 
I also tried replacing echo with print to no avail. Thanks for the help so far...

- Merg
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: PHP within a string of HTML

Post by Syntac »

My example was only to demonstrate the following point: Printing a string does not execute any code it contains. I wasn't telling you to use it in your script.
Post Reply