Whereas variables are simple statments of equivalence (A=B), functions allow for complex logic and other features that you can't get with simple variables. We won't get into any complex logic in this class, but we will use functions to display long passages of XHTML markup. To create a function, you have to designate it as such, then put parentheses at the end of the function name. In more complex functions, the parentheses are used to pass variables to the functions. We won't do that in our example here though. We're going to keep it simple. The actual content of the function starts with an opening brace ( { ) and closes with a closing brace ( } ). Here is an example of an empty function called "content," before putting the actual content into it:
<?php function content(); { } ?>
In our case, we're going to insert some XHTML content in the middle of the function, which means that we have to end the PHP code, start XHTML markup, then start PHP back up again, like this:
<?php function content(); { ?> <p>XHTML markup goes here.</p> <?php } ?>
Using functions like this allows us to have regular XHTML markup in the middle without having to worry about which quotation marks to use, or whether to use backslashes all over the place to "escape" quotation marks or apostrophes.
All we've done so far is declare the value of the function, but we haven't yet done anything to actually use that value, so here's an example of how to use a function in the middle of a web document:
<p>Here is a paragraph before the function.</p> <?php content(); ?> <p>Here is a paragraph after the function.</p>
If the value of the function is the one in our example, the end product would look like this after the server is done processing the PHP code:
<p>Here is a paragraph before the function.</p> <p>XHTML markup goes here.</p> <p>Here is a paragraph after the function.</p>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.