PHP Syntax

A PHP script starts with <?php and ends with ?>, and every statement ends with a semicolon.

Syntax<?php statement; statement;

A PHP script can be placed anywhere in a document. It always starts with <?php and ends with ?>.

Statements

Each instruction is a statement and must end with a semicolon ;. The two most common ways to send output are echo and print.

When a file contains only PHP, the closing ?> tag is optional and often left out.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Ending each instruction so PHP knows where one statement stops.
  • Switching between PHP logic and HTML output in the same file.
  • Writing short echo tags inside templates.

More examples

Statements end with ;

Every statement finishes with a semicolon.

Example · php
<?php
$a = 5;
$b = 10;
echo $a + $b;

Open and close tags

<?php opens PHP and ?> returns to plain HTML/text.

Example · php
<?php
echo "Hi";
?>
<!-- back to plain HTML -->

Short echo tag

<?= ... ?> is shorthand for <?php echo ... ?>.

Example · php
<p>Total: <?= 3 * 4 ?></p>

Discussion

  • Be the first to comment on this lesson.