0Pricing
PHP Academy · Lesson

Declaring Variables in PHP

Learn how PHP variables work with the $ prefix and dynamic typing.

Declaring Variables in PHP is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Variable?

A variable is a named container that holds a value your program can read and change.

  • In PHP every variable name starts with a dollar sign: $name
  • Variable names are case-sensitive: $age and $Age are different
  • Names must start with a letter or underscore, not a digit

PHP is a dynamically typed language — you never declare the type; PHP figures it out from the value you assign.

Assigning Values

Use the = operator to assign a value to a variable:

<?php
$name = 'Alice';
$age  = 30;
$pi   = 3.14159;
$active = true;

echo $name;   // Alice
echo $age;    // 30

PHP Is Dynamically Typed

You can reassign a variable to a completely different type — PHP allows it:

<?php
$value = 42;        // integer
$value = 'hello';   // now a string — perfectly valid
$value = true;      // now a boolean

echo gettype($value); // boolean

Naming Conventions

PHP doesn't enforce a naming style, but the community follows these conventions:

  • camelCase for variables: $userName, $totalPrice
  • snake_case is also common: $user_name
  • Constants use ALL_CAPS: define('MAX_SIZE', 100)
  • Avoid generic names like $a or $tmp in real code

Variable Variables

PHP has a quirky feature: a variable variable uses the value of one variable as the name of another:

<?php
$varName = 'city';
$$varName = 'Istanbul';  // creates $city

echo $city;   // Istanbul
echo $$varName; // Istanbul

Constants with define() and const

Constants hold values that never change. PHP offers two ways to define them:

<?php
define('APP_VERSION', '2.0.1');  // runtime constant
const DB_HOST = 'localhost';      // compile-time constant

echo APP_VERSION;  // 2.0.1
echo DB_HOST;      // localhost
// APP_VERSION = '3.0'; // ERROR — constants can't be changed

Checking if a Variable Exists

Before using a variable, check whether it has been set to avoid undefined variable notices:

  • isset($var) — true if the variable exists and is not NULL
  • empty($var) — true if the variable is falsy (0, '', false, [], NULL)
  • unset($var) — destroys a variable

isset and empty in Practice

Using isset and empty to guard variable access:

<?php
$username = '';

if (!isset($username)) {
    echo 'Variable does not exist';
} elseif (empty($username)) {
    echo 'Username is empty';   // this runs
} else {
    echo 'Hello, ' . $username;
}

gettype() and var_dump()

Inspect a variable's type and value during development:

<?php
$score = 98.5;

echo gettype($score);  // double
var_dump($score);      // float(98.5)

$items = ['a', 'b'];
var_dump($items);
// array(2) { [0]=> string(1) "a" [1]=> string(1) "b" }

Reference Assignment

By default PHP copies values. Use & to assign by reference so both variables point to the same data:

<?php
$a = 10;
$b = &$a;  // $b is a reference to $a

$b = 20;
echo $a;  // 20 — changed through $b

Predefined Superglobals

PHP provides several built-in superglobal arrays available everywhere in your script:

  • $_GET — URL query parameters
  • $_POST — form submission data
  • $_SESSION — session data
  • $_SERVER — server and execution environment info
  • $_ENV — environment variables

You'll use these constantly in web PHP development.

Quick Check

Which of the following is a valid PHP variable name?

Recap: Variables in PHP

Key takeaways about PHP variables:

  • All variable names start with $
  • PHP is dynamically typed — the type follows the value
  • Use isset() before accessing potentially unset variables
  • Use gettype() and var_dump() to debug
  • Assign by reference with & when you need aliasing
  • Superglobals like $_POST are available everywhere

Next: the data types PHP supports and how to work with each one.

Frequently asked questions

Is the “Declaring Variables in PHP” lesson free?

Yes — the full text of “Declaring Variables in PHP” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring Variables in PHP”?

Learn how PHP variables work with the $ prefix and dynamic typing. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Variables in PHP” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Variables in PHP
  2. PHP Data Types Overview
  3. Echo and Print Output
  4. Type Juggling and Type Casting
← Back to PHP Academy