PHP Syntax and Comments
Syntax refers to the set of rules that define how code must be written for the computer to understand it. PHP has its own syntax rules — specific ways to structure statements, use tags, and separate instructions. Breaking these rules causes errors. Understanding them from the start prevents a lot of frustration later.
PHP Tags
PHP code must be placed inside PHP tags so the server knows which parts of a file to interpret as PHP. The standard PHP tags are:
<?php
// PHP code goes here
?>
The opening tag is <?php and the closing tag is ?>. Everything between these tags is treated as PHP code. Everything outside them is treated as plain HTML and passed directly to the browser.
Short Echo Tag
PHP also supports a shorthand for outputting values directly:
<?= "Hello!" ?>
This is equivalent to writing <?php echo "Hello!"; ?>. It is only recommended when the sole purpose of the PHP block is to output a value.
Statements and Semicolons
Each PHP statement must end with a semicolon (;). A statement is a single instruction — outputting text, assigning a variable, calling a function, and so on. Forgetting the semicolon is one of the most common mistakes in PHP.
<?php
echo "This is the first statement.";
echo "This is the second statement.";
?>
Each line ends with a semicolon. PHP does not use line breaks to separate statements — only the semicolon matters.
What Happens Without a Semicolon
<?php
echo "Missing semicolon"
echo "This will cause an error"
?>
The code above causes a parse error because the first statement is not terminated. PHP expects either a semicolon or the end of the file after each instruction.
Case Sensitivity in PHP
PHP is partially case-sensitive, which means casing matters in some places but not others.
- Keywords are case-insensitive —
echo,ECHO, andEchoall work the same way. - Variable names are case-sensitive —
$nameand$Nameare two completely different variables. - Function names are case-insensitive —
strlen()andSTRLEN()produce the same result.
<?php
$color = "blue";
$Color = "red";
echo $color; // Outputs: blue
echo $Color; // Outputs: red
?>
By convention, always write keywords and function names in lowercase to keep code consistent and readable.
Whitespace and Formatting
PHP ignores extra spaces, tabs, and blank lines between statements. This allows code to be formatted for readability without affecting its behavior.
<?php
$a = 10;
$b = 20;
$sum = $a + $b;
echo $sum;
?>
The blank line between the variable declarations and the addition is ignored by PHP. It is there only to improve readability.
Comments in PHP
Comments are lines in the code that PHP ignores completely. They exist solely for the programmer — to explain what the code does, leave notes, or temporarily disable a line during testing.
Single-Line Comments
Two styles work for single-line comments:
<?php
// This is a single-line comment using double slashes
# This is also a single-line comment using a hash
echo "Comments are invisible to the browser.";
?>
Everything after // or # on the same line is ignored by PHP.
Multi-Line Comments
For comments that span multiple lines, use the /* */ syntax:
<?php
/*
This is a multi-line comment.
It can span as many lines as needed.
Useful for describing blocks of code.
*/
echo "Multi-line comments work great for documentation.";
?>
Inline Comments
<?php
$price = 49.99; // Price in dollars
$quantity = 3; // Number of items
$total = $price * $quantity;
echo $total; // Outputs: 149.97
?>
Inline comments appear on the same line as code. They explain what that specific line does without interrupting the flow.
Key Points
- PHP code is placed between
<?phpand?>tags. - Every PHP statement must end with a semicolon
;. - Keywords and function names are case-insensitive; variable names are case-sensitive.
- Extra whitespace is ignored and can be used freely to improve readability.
- Single-line comments use
//or#. - Multi-line comments use
/* */. - Comments are skipped by PHP and never sent to the browser.
