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.