How do I insert PHP into an HTML page?
PHP and HTML are completely distinct languages. You need to help your server tell them apart so that it knows which parts to process as PHP. Always open PHP code with
<?php and end it with
?>. You can do this as many times as you want in your document.
?> must use correct PHP syntax, or else you'll get one of those dreaded "parse errors" that can be very difficult to figure out when you're new to PHP. So let's talk a little about correct syntax.
First, let me explain how to insert comments. You know that something like
<!-- comment --> in an HTML document gets ignored because it's a comment. There are a few ways to do the same thing in PHP. Here are the two you'll see used in themes most frequently, the
⁄⁄ syntax and the
⁄* ... *⁄ syntax:
<?php $myVar1 = 'How are you?'; ⁄⁄ two slashes indicate that everything until the end of the line is a comment $myVar2 = 'Well, you?'; ⁄* if your comment spans more than one line, like this one, use the slash-asterisk format. *⁄ $myVar3 = 'Well.'; ?>
When editing themes (or plugins), you'll encounter variables, functions, and statements. Each has its own syntax, so we'll spend a section on each.