0Pricing
PHP Academy · Lesson

Variables and Constants

Learn how to use variables and constants in PHP.

Variables and Constants is a free PHP Academy lesson on CoddyKit — lesson 1 of 5. 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 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Variables and Constants

Welcome to a new chapter in your PHP journey! In this lesson, you’ll learn how to use variables to store data and constants to define unchangeable values. Let’s get started!

Variables and Constants — illustration 1

2

What Are Variables?

Variables are used to store data that can change during the execution of a script. In PHP:

  • Variable names start with a $ symbol.
  • They can store different types of data like strings, numbers, and arrays.

Example:

<?php
  $name = "Alice";
  $age = 25;
  echo "Name: $name, Age: $age";
?>

3

Rules for Variable Names

Follow these rules when naming variables in PHP:

  • Variable names must start with a letter or an underscore (_).
  • They can contain letters, numbers, and underscores, but no spaces.
  • Variable names are case-sensitive.

Example:

<?php
  $name = "Alice";
  $Name = "Bob";
  echo $name; // Outputs: Alice
  echo $Name; // Outputs: Bob
?>

4

Data Types in Variables

PHP variables can store data of various types, including:

  • String: Text data (e.g., "Hello").
  • Integer: Whole numbers (e.g., 42).
  • Float: Decimal numbers (e.g., 3.14).
  • Boolean: True or false values.
  • Array: A collection of values.

Example:

<?php
  $string = "Hello, World!";
  $integer = 42;
  $float = 3.14;
  $boolean = true;
  echo $string; // Outputs: Hello, World!
?>

5

What Are Constants?

Constants are used to store values that cannot be changed during the script execution. Use the define() function to declare constants.

Example:

Tip: Constants are automatically global and can be accessed from any part of your script.

<?php
  define("SITE_NAME", "My Website");
  echo SITE_NAME; 
// Outputs: My Website
?>

6

Constants vs Variables

Here are the key differences between constants and variables:

  • Constants do not use the $ symbol, while variables do.
  • Constants cannot be redefined once set, but variable values can change.
  • Constants are always global, while variables can have local or global scope.

Example:

<?php
  define("MAX_USERS", 100);
  $current_users = 5;
  echo MAX_USERS; // Outputs: 100
  echo $current_users; // Outputs: 5
?>

7

Using Variables and Constants Together

You can combine variables and constants to build dynamic outputs. Example:

Tip: Use the concatenation operator (.) to combine strings in PHP.

<?php
  define("GREETING", "Welcome");
  $name = "Alice";
  echo GREETING . ", " . $name . "!"; 
// Outputs: Welcome, Alice!
?>

8

Best Practices

Follow these best practices for using variables and constants:

  • Use meaningful names to describe their purpose (e.g., $userName).
  • Use constants for values that do not change (e.g., define("PI", 3.14);).
  • Group related variables together to improve code readability.

Tip: Consistent naming conventions make your code easier to understand and maintain.

9

10

Great Job!

Congratulations! You’ve learned how to use variables and constants in PHP to store and manage data. These tools are fundamental to building dynamic and flexible web applications. In the next lesson, we’ll explore PHP data types in more detail. Let’s keep coding!

Variables and Constants — illustration 10

Frequently asked questions

Is the “Variables and Constants” lesson free?

Yes — the full text of “Variables and Constants” is free to read here on the web, and the PHP Academy course includes 5 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 “Variables and Constants”?

Learn how to use variables and constants in PHP. 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 5, so you can start here or from the beginning and move at your own pace.

How long does the “Variables and Constants” 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. Variables and Constants
  2. Data Types
  3. Operators
  4. Control Structures
  5. Working with Arrays
← Back to PHP Academy