beginner Step 2 of 16

Variables, Constants, and Operators

PHP Programming

Variables, Constants, and Operators

PHP variables, constants, and operators form the foundation of every PHP program. Unlike JavaScript or Python, PHP requires the dollar sign prefix for all variables. PHP supports a rich set of operators including arithmetic, comparison, logical, string, and the spaceship operator. Understanding variable scope in PHP is particularly important because PHP has different scoping rules than most other languages — variables inside functions do not automatically have access to variables defined outside them.

Variables and Scope

<?php
// Variable declaration (no type keyword needed)
$name = "Alice";
$age = 30;
$price = 19.99;
$is_active = true;

// Variable variables (dynamic variable names)
$field = "name";
$$field = "Bob";  // Creates $name = "Bob"
echo $name;  // "Bob"

// Variable scope — PHP has function-level scoping
$global_var = "I'm global";

function myFunction() {
    // $global_var is NOT accessible here!
    // echo $global_var;  // Warning: Undefined variable

    // Use 'global' keyword to access
    global $global_var;
    echo $global_var;  // "I'm global"

    // Or use $GLOBALS superglobal
    echo $GLOBALS['global_var'];

    // Local variables
    $local = "I'm local";
}

myFunction();
// echo $local;  // Warning: Undefined variable

// Static variables (persist between function calls)
function counter() {
    static $count = 0;
    $count++;
    return $count;
}
echo counter();  // 1
echo counter();  // 2
echo counter();  // 3
?>

Constants

<?php
// define() — traditional way
define("MAX_SIZE", 100);
define("APP_NAME", "MyApp");
echo MAX_SIZE;  // 100 (no $ prefix)

// const — modern way (preferred in classes)
const PI = 3.14159;
const DB_HOST = "localhost";

// Class constants
class Config {
    const VERSION = "1.0.0";
    const MAX_RETRIES = 3;
}
echo Config::VERSION;  // "1.0.0"

// Enum (PHP 8.1+)
enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
    case Pending = 'pending';
}
$status = Status::Active;
echo $status->value;  // "active"
?>

Operators

<?php
// Arithmetic
$a = 10; $b = 3;
echo $a + $b;   // 13
echo $a - $b;   // 7
echo $a * $b;   // 30
echo $a / $b;   // 3.333...
echo $a % $b;   // 1
echo $a ** $b;  // 1000

// String concatenation (uses . not +)
$first = "Hello";
$last = "World";
echo $first . ", " . $last . "!";  // "Hello, World!"
$first .= " there";  // "Hello there" (concatenation assignment)

// Comparison
echo 5 == "5";    // true (loose comparison — type juggling)
echo 5 === "5";   // false (strict comparison — type + value)
echo 5 != "5";    // false
echo 5 !== "5";   // true
echo 5 <=> 3;     // 1 (spaceship operator: -1, 0, or 1)

// Null coalescing
$config = [];
$port = $config['port'] ?? 8080;  // 8080 (if null or undefined)
$host = $config['host'] ?? "localhost";

// Null coalescing assignment
$settings['theme'] ??= 'light';

// Ternary
$age = 20;
$status = ($age >= 18) ? "adult" : "minor";

// Logical
$a = true; $b = false;
echo $a && $b;   // false
echo $a || $b;   // true
echo !$a;        // false

// Type operators
echo ($name instanceof User);  // Check object type
?>
Pro tip: Always use strict comparison (=== and !==) in PHP. The loose comparison operator (==) has notoriously surprising type juggling rules — for example, "0" == false is true, "" == false is true, but "0" == "" is false. Strict comparison avoids all of these gotchas.

Key Takeaways

  • PHP variables use the $ prefix and have function-level scope; use global or $GLOBALS for global access.
  • Constants are defined with const or define() and do not use the $ prefix.
  • String concatenation uses the dot operator (.), not + as in most other languages.
  • Always use strict comparison (===) to avoid PHP's type juggling surprises.
  • The null coalescing operator (??) and spaceship operator (<=>) are powerful modern PHP features.