PHPLesson 3 of 13

PHP Syntax and Echo

PHP code lives between two markers: <?php opens a block and ?> closes it. Everything inside is executed by the server, and everything outside is plain HTML that passes through untouched. This is why PHP files look like HTML with occasional islands of code in the middle.

PHP code lives between two markers: <?php opens a block and ?> closes it. Everything inside is executed by the server, and everything outside is plain HTML that passes through untouched. This is why PHP files look like HTML with occasional islands of code in the middle.

The echo statement is how PHP prints text into the page. Whatever you echo lands exactly where the block sits in the HTML. You can echo a word, a number, or a whole chunk of markup. The browser receives it as if the HTML had been written by hand.

Statements in PHP end with a semicolon. Forgetting it is the most common beginner error, and PHP answers with a parse error that stops the whole file. The error message names the file and line, so reading it tells you exactly where to look.

Comments use // for one line and /* */ for blocks. PHP ignores them, but they keep your code readable. Write a comment explaining the why, not the what, and your future self will thank you.

Echo with and without parentheses

echo can be written with or without parentheses, and most code skips them. print is a similar command that returns a value, which is why echo is more common. Both accept a string or a number.

PHP
<?php
echo "No parentheses here";
echo("With parentheses also works");
print "print does the same job";
?>

Mixing HTML and PHP

The real power is the mix. You can open a PHP block in the middle of a page, echo some dynamic text, and close it again. The browser only ever sees the final HTML.

PHP
<h1>My page</h1>
<p>Today is <?php echo date("l"); ?>.</p>

Syntax

PHP
<?php
echo "Hello, world!"; // statements end with a semicolon
?>

Missing Semicolon Breaks Next Line

PHP requires ; at end of each statement — missing one breaks the next line with parse error.

PHP
$a = 5; $b = 6; // ; required

Good to know

  • PHP code lives between <?php and ?>.
  • echo sends text into the page at the spot it appears.
  • Every statement ends with a semicolon.
  • Comments start with // or sit inside /* and */.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground