Variable Types in PHP

PHP Variable Types: Complete Beginner’s Guide to PHP Data Types

Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP tutorials. If you’re just starting with PHP or looking to solidify your foundation, understanding variable types in PHP is essential.

In this complete course page, we’ll walk through every primary PHP data type, how variables are defined and used, and provide clear, real-world examples.


What Is a Variable in PHP?

A variable in PHP is a container used to store data, such as numbers, text, arrays, or objects. Variables in PHP start with a $ (dollar sign) followed by the variable name.

$greeting = "Hello, World!";

Rules for Naming Variables:

  • Must start with a $ symbol
  • Followed by a letter or underscore ( _ )
  • Cannot start with a number
  • Are case-sensitive (i.e., $Var and $var are different)

Types of Variables in PHP

PHP is a loosely typed language, meaning you don’t need to declare the data type of a variable before assigning a value. PHP automatically converts the variable to the correct data type.

However, PHP supports eight primary data types:

  1. String
  2. Integer
  3. Float (Double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource

Let’s break each of these down.


1. PHP String

A string is a sequence of characters enclosed in quotes (single or double).

Example:

$name = "John";
$message = 'Welcome to PHP!';
echo $name . ", " . $message;

String Functions:

  • strlen() – get length
  • strtoupper() – convert to uppercase
  • strtolower() – convert to lowercase
  • strpos() – find position of a substring
echo strlen("Hello"); // Outputs 5

2. PHP Integer

An integer is a whole number (positive or negative) without a decimal point.

Example:

$age = 25;
echo $age;

Valid Integer Ranges:

  • Platform dependent (commonly -2,147,483,648 to 2,147,483,647 on 32-bit systems)

3. PHP Float (Double)

A float (or double) is a number with a decimal point or in exponential form.

Example:

$price = 19.99;
echo $price;

4. PHP Boolean

A boolean represents two possible states: true or false.

Example:

$isLoggedIn = true;
if ($isLoggedIn) {
    echo "Welcome!";
}

Booleans are often used in conditional statements.


5. PHP Array

An array is a variable that holds multiple values.

Indexed Array Example:

$colors = array("Red", "Green", "Blue");
echo $colors[0]; // Outputs Red

Associative Array Example:

$user = array("name" => "Alice", "email" => "alice@example.com");
echo $user["name"];

Multidimensional Array Example:

$students = array(
  array("Alice", 22),
  array("Bob", 23),
);
echo $students[1][0]; // Outputs Bob

6. PHP Object

Objects are instances of classes. They are used in object-oriented programming (OOP).

Example:

class Car {
    public $color;
    public function setColor($c) {
        $this->color = $c;
    }
    public function getColor() {
        return $this->color;
    }
}

$myCar = new Car();
$myCar->setColor("Blue");
echo $myCar->getColor();

7. PHP NULL

The NULL data type represents a variable with no value.

Example:

$x = NULL;

Useful when you want to explicitly empty a variable or check whether a value exists.


8. PHP Resource

Resources are special variables used to hold references to external resources like database connections.

Example (MySQLi connection):

$conn = mysqli_connect("localhost", "root", "", "myDB");

You don’t directly manipulate resource types like you do with strings or integers.


PHP Type Checking Functions

PHP provides built-in functions to check variable types:

  • is_string()
  • is_int()
  • is_float()
  • is_bool()
  • is_array()
  • is_object()
  • is_null()

Example:

$test = 123;
if (is_int($test)) {
  echo "This is an integer.";
}

PHP Type Casting

You can cast variables to a specific type using type casting.

Example:

$price = "99.99";
$price = (float)$price;

You can cast to:

  • (int) or (integer)
  • (bool) or (boolean)
  • (float), (double) or (real)
  • (string)
  • (array)
  • (object)
  • (unset) (deprecated)

Variable Scope in PHP

  • Local: Declared inside a function
  • Global: Declared outside all functions
  • Static: Retains value between function calls

Global Example:

$x = 5;
function myTest() {
  global $x;
  echo $x;
}

Constants vs Variables

Constants are similar to variables, but:

  • They do not begin with $
  • Are defined using define() or const
  • Cannot be changed once set

Example:

define("SITE_NAME", "phponline.in");
echo SITE_NAME;

PHP 7+ Type Declarations (Optional)

In modern PHP (7 and later), you can declare function argument and return types.

Example:

function add(int $a, int $b): int {
  return $a + $b;
}

This helps ensure type safety and better code quality.


Real-World Examples Using Variable Types

Example 1: User Profile

$name = "Amit";
$age = 28;
$isActive = true;
$skills = array("PHP", "JavaScript", "HTML");

Example 2: E-Commerce Cart

$cartTotal = 99.95;
$discount = NULL;
$items = array("Shoes" => 1, "Socks" => 2);


PHP variable types, PHP data types, PHP string, PHP integer, PHP float, PHP boolean, PHP object, PHP array, PHP tutorial for beginners

Internal Links for Extended Learning


Frequently Asked Questions (FAQ)

What is a variable in PHP?

A variable is a named container used to store data like strings, integers, or arrays.

How many data types does PHP support?

PHP supports eight main data types: string, integer, float, boolean, array, object, NULL, and resource.

Can I change a variable’s data type?

Yes, PHP allows you to change a variable’s type by simply assigning a new value.

Is PHP strictly typed?

No. PHP is loosely typed, but type declarations are supported in newer versions.

What is the difference between NULL and an empty string?

NULL means no value. An empty string is a value, but just an empty one.

Are variable names case-sensitive in PHP?

Yes, $Name, $NAME, and $name are considered different.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments