PHP Syntax and Variables
Every PHP script begins with an opening tag, <?php, and optionally ends with ?> (the closing tag is often omitted in pure-PHP files to avoid accidental whitespace output). Statements end with a semicolon, blocks are delimited with curly braces, and PHP is case-sensitive for variable names but case-insensitive for function and class names (though relying on the latter is poor practice). Variables are declared simply by assigning to a name prefixed with a dollar sign — there is no separate declaration keyword like var or let, and PHP is dynamically typed by default, meaning a variable's type is determined by whatever value it currently holds.
Cricket analogy: The <?php opening tag is like the umpire signaling the start of play — everything after it counts; semicolons ending statements are like each ball being logged discretely, and $x = 5; needing no 'var' keyword is like a batter's score simply being whatever the last ball produced.
Naming Rules and Variable Scope
A valid variable name starts with a letter or underscore, followed by any combination of letters, numbers, or underscores — $1name is invalid, $_name and $name1 are fine. PHP variables are function-scoped by default: a variable created inside a function is invisible outside it, and a variable in the global scope is invisible inside a function unless explicitly imported with the global keyword or, more idiomatically, passed as a parameter. Superglobals like $_GET, $_POST, and $_SESSION are the exception — they are accessible in every scope without any special declaration.
Cricket analogy: Function scoping is like a substitute fielder brought on only for the duration of an over — invisible to the rest of the match once it ends — while superglobals like $_SESSION are like the umpire's decisions, visible and binding throughout the entire game regardless of who's fielding.
<?php
declare(strict_types=1);
$count = 10; // integer
$price = 19.99; // float
$label = "Widget"; // string, double-quoted (supports interpolation)
$isActive = true; // boolean
$tags = ['new', 'sale']; // indexed array
function applyDiscount(float $price, float $rate): float {
// $count from the outer scope is NOT visible here
return $price - ($price * $rate);
}
echo "Original: {$label} costs \${$price}\n";
echo "Discounted: " . applyDiscount($price, 0.1) . "\n";
Comments and Whitespace
PHP supports three comment styles: // and # for single-line comments, and /* ... */ for multi-line comments. Whitespace outside of string literals is generally insignificant, but indentation conventions (commonly PSR-12) matter enormously for readability and are enforced by tools like PHP-CS-Fixer in professional codebases.
Cricket analogy: PHP's // and /* */ comment styles are like a scorer's shorthand notes versus a full match report — quick single-line jottings ('wide, 1 run') versus a detailed multi-paragraph summary, and PSR-12 formatting is like the standardized scorebook layout every official scorer follows.
A handy mnemonic: PHP variable names are like JavaScript variables but always prefixed with $, and the language never requires you to declare a type ahead of time — 'strict_types' only affects how function argument and return types are checked, not variable declarations themselves.
Leaving a stray blank line or space after the closing ?> tag in a file that only contains PHP (especially one that sets HTTP headers) is a classic bug: that whitespace is sent as output before your headers, triggering 'headers already sent' errors. The fix most teams adopt is to simply omit the closing ?> tag entirely.
- Scripts start with <?php; the closing ?> is optional and often omitted in pure-PHP files.
- Variables are prefixed with $, are dynamically typed, and need no declaration keyword.
- Variable names are case-sensitive; they must start with a letter or underscore.
- Variables are function-scoped by default — use parameters (not the
globalkeyword) to share data cleanly. - Superglobals like $_GET and $_SESSION are accessible from any scope without import.
- declare(strict_types=1) enforces strict type checking on function signatures, not on plain variable assignment.
Practice what you learned
1. Which variable name is invalid in PHP?
2. Are PHP variable names case-sensitive?
3. By default, can a variable declared inside a function be accessed outside it?
4. What does declare(strict_types=1) actually affect?
5. Which of these is NOT a valid PHP comment syntax?
Was this page helpful?
You May Also Like
What Is PHP?
An introduction to PHP as a server-side scripting language: its history, how it executes on the web, and why it still powers a huge share of the internet.
Data Types in PHP
A tour of PHP's scalar, compound, and special types, plus how PHP's dynamic typing and type juggling behave in practice.
Operators and Expressions
Explains PHP's arithmetic, comparison, logical, assignment, and null-coalescing operators, along with operator precedence and expression evaluation order.
Functions in PHP
Learn how PHP functions are declared, typed, and invoked, including default parameters, variadics, references, and closures for reusable logic.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics