100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
PHP

PHP Syntax and Variables

Covers PHP's basic script structure, statement termination, comments, and how variables are declared, scoped, and named in PHP 8.x.

PHP FoundationsBeginner7 min readJul 9, 2026
Analogies

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
<?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 global keyword) 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

Was this page helpful?

Topics covered

#PHP#PHPProgrammingStudyNotes#Programming#PHPSyntaxAndVariables#Syntax#Variables#Naming#Rules#StudyNotes#SkillVeris