PHP tags
Learn to embed PHP code in HTML with <?php and ?> tags.
PHP tags
To embed PHP code in HTML you use PHP tags. The standard opening tag is <?php and the closing tag is ?>.
Everything between these tags is interpreted as PHP code. Anything outside the tags is output as plain text.
Example: <?php 42 ?> is a valid PHP block. The <?php tag works on every server; short tags like <? may be disabled on some servers.
Closing tag and plain text
A PHP block opens with <?php and closes with ?>. Everything after the closing tag is output as plain text.
If a file contains only PHP code, omitting the closing tag is recommended; this avoids unwanted whitespace at the end of the file.
You can mix PHP and HTML in the same file. Example: <p><?php 42 ?></p> — here <p> is HTML and <?php 42 ?> is a PHP block.
Test yourself
-
Which is the standard PHP opening tag?
- <?php
- <php>
- %php%
- [php]
Show answer
<?php — Correct! <?php is the standard tag that works on every server.
-
Write an expression between the tags
<?php ____ ?>Show answer
<?php 42 ?> -
Which is a complete PHP block?
- <php 42 </php>
- <?php 42;
- <% 42 %>
- <?php 42 ?>
Show answer
<?php 42 ?> — Correct! <?php 42 ?> has both an opening and a closing tag.
-
Match the PHP tags
- <?php
- Standard opening
- ?>
- Closing tag
- <?
- Short open tag