PHP Basics: Syntax and echo
Site Admin
· 11 Sep 2026
· 9 views
PHP Basics: Syntax and echo
PHP syntax is approachable, mixing HTML and embedded script blocks. echo is the workhorse that prints output.
Tags and statements
<?php
echo 'First line';
echo 'Second line';
Statements end with a semicolon. The closing tag is optional when the file is pure PHP, and omitting it avoids stray whitespace in output.
echo with strings
echo prints anything scalar. Single quotes treat text literally, while double quotes interpret variables and escapes:
<?php
$name = 'Ada';
echo 'Hello $name'; // prints literally
echo "Hello $name"; // prints value
Comments use // for single lines and slash-star for blocks.
Concatenation
The dot operator joins strings:
echo 'Total: ' . ($total + 1);
PHP and HTML together
<h1>Products</h1>
<?php foreach ($products as $p): ?>
<p><?= $p ?></p>
<?php endforeach; ?>
The short tag, ?=, is a compact echo. Alternating between HTML and PHP like this is normal in template-style scripts.
Debugging output
var_dump reveals a value and its type, which is invaluable while learning:
var_dump($user);
Key Points
- Statements end with semicolons.
- echo prints strings and numbers.
- Single quotes are literal; double quotes interpolate.
- The dot operator concatenates strings.
- var_dump inspects values during debugging.