PHP Variables and Data Types

Introduction

If you’ve understood what PHP is and how it works, the next step is learning how to store and work with data. In PHP, this is done using variables and data types. These concepts form the foundation of every PHP program you’ll ever write.

Whether you’re building forms, working with databases, or creating features for platforms like WordPress, you’ll constantly use variables to store values and data types to control how those values behave.

By the end of this tutorial, you’ll know how to declare variables in PHP, understand the most common data types, and avoid beginner mistakes.

What Is a Variable in PHP?

A variable in PHP is used to store data that can be used and changed later in the program.
Variables help us store values such as numbers, text, or other data types.

In PHP, a variable always starts with the $ (dollar) symbol.

<?php
$name = "Divyesh";
$age = 30;
?>

In this example:

  • $name stores a string value
  • $age stores a number

PHP is a loosely typed language, which means you don’t need to specify the data type when declaring a variable. PHP figures it out automatically based on the value you assign.Rules for Creating Variables in PHP.

Rules for Naming PHP Variables

When creating variables in PHP, keep these rules in mind:

  • A variable must start with $
  • The variable name must start with a letter or underscore
  • Variable names cannot start with a number
  • Variable names are case-sensitive
  • Can contain letters, numbers, and underscores

Valid Examples:

$name = "John";
$_age = 25;
$totalMarks = 90;

Invalid Example:

$1name = "John"; // Invalid

Assigning Values to Variables

You can assign different types of values to variables.

<?php
$name = "PHP";
$version = 8;
$isActive = true;
?>

What Are Data Types in PHP?

A data type defines what kind of data a variable holds.
PHP supports several built-in data types.

Common PHP Data Types

1️⃣ String

A string is used to store text data.

<?php
$message = "Hello PHP";
?>

2️⃣ Integer

An integer is used to store whole numbers.

<?php
$number = 100;
?>

3️⃣ Float

A float is used to store decimal numbers.

<?php
$price = 49.99;
?>

4️⃣ Boolean

A boolean stores true or false values.

<?php
$isLoggedIn = true;
?>

5️⃣ Array

An array stores multiple values in a single variable.

<?php
$colors = array("Red", "Green", "Blue");
?>

6️⃣ NULL

A NULL value means no value is assigned.

<?php
$result = null;
?>

Checking Variable Data Type

You can use the var_dump() function to check a variable’s type and value.

<?php
$value = 10;
var_dump($value);
?>

Output:

int(10)

PHP Is Loosely Typed

PHP is a loosely typed language, which means you do not need to declare the data type explicitly.

<?php
$data = 10;
$data = "PHP";
?>

The variable $data changes its type automatically.

Why Variables and Data Types Matter

Understanding variables and data types helps you:

  • Store user input from forms
  • Perform calculations
  • Control program logic
  • Work with databases
  • Build dynamic web pages

Without this knowledge, writing meaningful PHP programs becomes very difficult.

Variable Scope (Basic Introduction)

Scope defines where a variable can be accessed.

  • Local scope – defined inside a function
  • Global scope – defined outside a function

Examples for Local Scope and Global Scope:

Example 1: Local Scope in PHP

<?php
function showName() {
    $name = "DivPHP Tutorials";  // Local variable
    echo $name;
}

showName();   // Output: DivPHP Tutorials
echo $name;   // Error: Undefined variable
?>

Explanation:

  • $name is created inside showName()
  • It works only inside that function
  • Trying to use $name outside causes an error

Example 2: Global Scope in PHP (Problem Case)

A variable declared outside a function has global scope, but you cannot use it inside a function directly.

<?php
$siteName = "DivPHP Tutorials";  // Global variable

function showSite() {
    echo $siteName;  // Error: This will NOT work
}

showSite();
?>

Explanation:

  • $siteName is global
  • But PHP does NOT automatically allow global variables inside functions
  • This will throw an “undefined variable” notice

Example 3: Access Global Variable Using global Keyword

To use a global variable inside a function, use the global keyword:

<?php
$siteName = "DivPHP Tutorials";  // Global variable

function showSite() {
    global $siteName;   // Import global variable
    echo $siteName;
}

showSite();  // Output: DivPHP Tutorials
?>

Example 4: Access Global Variable Using $GLOBALS

Another way (less commonly used by beginners):

<?php
$siteName = "DivPHP Tutorials";

function showSite() {
    echo $GLOBALS['siteName'];
}

showSite();  // Output: DivPHP Tutorials
?>

Example 5: Function Parameter vs Global Variable (Best Practice)

Best practice is to pass variables into functions instead of using globals:

<?php
$siteName = "DivPHP Tutorials";

function showSite($name) {
    echo $name;
}

showSite($siteName);  // Output: DivPHP Tutorials
?>

Note: If functions are new to you, read our PHP Functions guide to learn how to create and use functions in PHP.

Best Practices for PHP Variables

  • Use meaningful variable names
  • Follow consistent naming conventions
  • Avoid unnecessary global variables
  • Keep variable names readable

Common Beginner Mistakes

1. Forgetting the $ symbol
PHP variables always start with $. Writing name = "John"; will cause an error.

2. Using invalid variable names
Avoid spaces, hyphens, or starting variable names with numbers.

3. Assuming PHP will always “fix” your mistakes
PHP is flexible, but wrong assumptions about data types can cause bugs.

4. Confusing strings and numbers
"10" (string) and 10 (integer) behave differently in some situations.

Conclusion

Variables and data types are the foundation of PHP programming.
Understanding them clearly will make it easier to learn conditions, loops, and functions in upcoming tutorials.

In the next tutorial, we’ll learn about PHP Operators and Expressions.

Related Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *