devnotes.

Variables and Constants

PHP variables start with a $ sign, followed by the variable name. PHP is dynamically typed — you don't declare a type, and a variable's type is inferred and can change at runtime.

$name = "Alice";        // string
$age = 30;               // int
$price = 19.99;          // float
$isActive = true;        // bool

$name = 42; // valid — PHP allows reassigning a different type

Variable naming rules: must start with a letter or underscore, followed by letters, numbers, or underscores; no spaces or hyphens. $user_name and $userName are both valid (snake_case and camelCase are both common conventions in the PHP world).

Constants don't change once defined, and don't use the $ prefix:

define('SITE_NAME', 'My App');
const MAX_USERS = 100; // preferred modern syntax, works at compile time

echo SITE_NAME;

PHP also provides useful predefined 'magic constants' like __LINE__, __FILE__, __FUNCTION__, and __CLASS__, which resolve to information about the current position in the code — handy for logging and debugging.

Variable scope matters: variables defined inside a function are local to it by default. Use the global keyword (rarely recommended) or, better, pass values as parameters and return values explicitly.

PHP Variables and Constants Tutorial | DevNotes