Variables, Data Types, and Constants in PHP

Table of Contents

  • What are Variables in PHP?
  • Rules for Naming PHP Variables
  • Variable Declaration and Assignment
  • PHP Data Types Explained
    • String
    • Integer
    • Float (Double)
    • Boolean
    • Array
    • Object
    • NULL
    • Resource
  • Type Juggling and Type Casting in PHP
  • What are Constants?
  • Defining Constants in PHP
  • Constants vs Variables
  • Summary

What are Variables in PHP?

In PHP, variables are used to store data that can be reused throughout your script. A variable can hold different types of values such as numbers, text, arrays, or even objects.

PHP variables start with a dollar sign ($) followed by the variable name.

$name = "John Doe";
$age = 30;

You don’t need to declare a data type beforehand—PHP automatically determines it based on the value assigned. This feature is known as dynamic typing.


Rules for Naming PHP Variables

  • Must begin with a dollar sign ($)
  • Must start with a letter or an underscore (_)
  • Cannot start with a number
  • Can contain letters, numbers, and underscores
  • PHP variables are case-sensitive

Valid Examples:

$firstName = "Alice";
$_counter = 100;

Invalid Examples:

$123name = "Bob"; // Invalid: starts with a number
$first-name = "Tom"; // Invalid: contains a hyphen

Variable Declaration and Assignment

You can declare and assign values to variables in a single line:

$greeting = "Hello, World!";
$price = 49.99;

You can also reassign values:

$price = 59.99;

PHP allows variable values to change types dynamically during execution.


PHP Data Types Explained

PHP supports a range of data types categorized into scalar, compound, special, and resource types.

1. String

A sequence of characters enclosed in quotes.

$text = "This is a string.";

2. Integer

Whole numbers (positive or negative).

$year = 2025;

3. Float (or Double)

Decimal numbers.

$price = 19.95;

4. Boolean

Logical true or false.

$isAvailable = true;

5. Array

A collection of values stored in a single variable.

$colors = array("red", "green", "blue");

6. Object

An instance of a class.

class Car {
public $model;
}
$myCar = new Car();

7. NULL

A special type representing a variable with no value.

$nothing = NULL;

8. Resource

Special variables holding references to external resources (e.g., database connections).


Type Juggling and Type Casting in PHP

PHP automatically converts a variable to the correct data type depending on its value and context. This is called type juggling.

$val = "10"; // string
$sum = $val + 5; // $sum becomes 15 (integer)

You can also explicitly convert data types using type casting:

$float = 10.56;
$intVal = (int)$float; // $intVal is 10

What are Constants?

Constants are like variables, but once defined, their value cannot be changed during script execution.

They are useful for values that remain the same throughout your program, such as database credentials or site settings.


Defining Constants in PHP

You can define a constant using the define() function:

define("SITE_NAME", "MyWebsite");
echo SITE_NAME;

From PHP 7 onwards, you can also use const inside classes:

class Config {
const VERSION = "1.0.0";
}

Constants vs Variables

FeatureVariableConstant
Starts with$No prefix
Can change?YesNo (immutable)
ScopeFunction and globalGlobal
Case-sensitive?YesYes (usually)

Summary

In this module, you learned how PHP handles variables, data types, and constants. PHP’s dynamic typing makes it flexible, but understanding the different data types and how they behave ensures more robust and error-free code.

You now know:

  • How to declare variables
  • The rules for naming variables
  • The eight core PHP data types
  • How to define and use constants

This knowledge will be crucial as you start manipulating data, creating logic, and building real applications.