Source: Telovation.com

Is it possible to use a heredoc as a function argument in PHP?  Yes.

Let’s say that you have a function that prints out the <head> section of an HTML page.  You want to use this function on all of your web pages since it helps keep things standardized; however, some pages may require additional things like JavaScript and/or CSS includes.

The heredoc string is a great way to insert custom text into a function without creating a separate variable for it.  Some pages may not need the additional text so it’s best to define the $includeText variable as optional as seen below.

/* Here is our example function */
function PrintHeader($title, $includeText=’’)
{
	print <<<HEREDOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
${includeText}
</head>\n
HEREDOC;

	return;
}

/* Here is how we can call it with heredoc as an argument */

PrintHeader('www.fleischkuekle.com’, <<<HEREDOC
<script language="JavaScript" type="text/javascript"
src="js/share/Ajax.js"></script>
<style type="text/css" media="screen">
	body {
		font: 11px arial;
	}
	.ajax_link {
		background-color: #FFFFFF;
		padding: 2px 6px 2px 6px;
	}
	.ajax_link_over {
		background-color: #3366CC;
		padding: 2px 6px 2px 6px;
	}
	#ajax_suggest {
		position: absolute;
		background-color: #FFFFFF;
		text-align: left;
		border: 1px solid #000000;
	}
</style>
HEREDOC
);
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)