Every programming language has a set of rules that define how to write and structure code. These rules are called **Syntax**. PHP's syntax is heavily inspired by C, Java, and Perl, making it very intuitive for developers coming from those backgrounds.
A PHP script can be placed anywhere in the document. A PHP script starts with
<?php and ends with ?>. Anything inside these tags is
processed by the server, while anything outside is sent directly to the browser as plain
text or HTML.
<?php
// Your PHP code goes here
echo "This is processed by the server.";
?>
In PHP, there is a very important distinction regarding case sensitivity. You must remember these two rules:
Built-in keywords (like if, else, echo) and
functions (like strtoupper()) are **not** case-sensitive. You can write
them in uppercase, lowercase, or mixed case.
<?php
ECHO "Hello World!";
echo "Hello World!";
EcHo "Hello World!";
?>
Unlike keywords, variable names are **strictly case-sensitive**. This means
$color, $Color, and $COLOR are three completely
different variables.
<?php
$color = "red";
echo "My car is " . $color; // Outputs: red
echo "My house is " . $Color; // Error: $Color is undefined
?>
In PHP, every statement must end with a semicolon (;). The
semicolon tells the PHP engine that one instruction is finished and the next one is
about to begin.
<?php
$x = 10; // Semicolon required
$y = 20; // Semicolon required
echo $x + $y; // Semicolon required
?>
PHP is generally indifferent to extra whitespace (spaces, tabs, newlines). This allows you to format your code in a way that is readable for humans.
<?php
// Both do exactly the same thing
echo "Compact style";
echo
"Spaced
style";
?>
<?php ... ?>.
echo, IF, and while
are not case-sensitive.$name and $Name are
different variables.;..php
extension to be processed by the server."Hello $name" inserts the value); single quotes don't ('Hello $name' prints literally, dollar sign and all).
?>. That extra whitespace gets sent to the browser as real output, which breaks header() redirects and sessions later in the request.
} reported on the wrong line. PHP often reports the parse error at the end of the file, not where the brace was actually forgotten — check your bracket pairing first when the error line looks wrong.
Run this to see the exact difference between single and double quotes for yourself:
<?php
$name = "Rafi";
echo "Hello, $name!"; // Hello, Rafi! (interpolated)
echo 'Hello, $name!'; // Hello, $name! (literal text)
?>
Q: Can I mix single and double quotes in the same script?
A: Yes, freely — just remember only double quotes interpolate variables, as shown above.
Q: Do I need spaces around operators like = or +?
A: No, PHP doesn't require them — $x=5+3; is valid — but spacing them out ($x = 5 + 3;) is the near-universal style convention and much easier to read.
Q: What happens if I forget a semicolon?
A: PHP throws a Parse error: syntax error, unexpected token and the entire script stops running — see the PHP manual's syntax reference for the exact rules.