PHP Data Types


In PHP, data types define the kind of data a variable can hold. Since PHP is a loosely typed language, you don’t need to explicitly declare the data type. PHP automatically determines it based on the value assigned. Understanding data types is essential because it affects how data is stored, manipulated, and displayed.

Common PHP Data Types

PHP has several built-in data types. The most commonly used are:

  1. String – sequences of characters

  2. Integer – whole numbers

  3. Float (Double) – decimal numbers

  4. Boolean – true or false

  5. Array – a collection of values

  6. Object – instances of classes

  7. NULL – represents no value

  8. Resource – holds external resources like database connections

String Data Type

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

<?php
// Using double quotes
$name = "Vicky"; 
echo "My name is $name<br>"; // Variable interpolation works

// Using single quotes
$city = 'Delhi';
echo 'I live in ' . $city . '<br>'; // Concatenation required
?>
  • Double quotes allow variable interpolation

  • Single quotes display the text literally

  • Strings can contain letters, numbers, and symbols

Integer Data Type

Integers are whole numbers without decimal points.

<?php
$age = 25; // Positive integer
$year = -2025; // Negative integer
echo "Age: $age<br>Year: $year<br>";
?>
  • Can be positive, negative, or zero

  • Used for counting, indexing, or calculations

Float / Double Data Type

Floats (also called doubles) are numbers with decimal points.

<?php
$price = 49.99;
$temperature = -12.5;
echo "Price: $" . $price . "<br>";
echo "Temperature: " . $temperature . "°C<br>";
?>
  • Used for precise measurements, prices, or scientific calculations

  • Can also be written in exponential form: $num = 1.5e3; (1500)

Boolean Data Type

Booleans have only two possible values: true or false.

<?php
$isAvailable = true;
$isFinished = false;

// Display boolean values
echo "Availability: " . $isAvailable . "<br>"; // Outputs 1 for true
echo "Finished: " . $isFinished . "<br>"; // Outputs nothing for false
?>
  • Often used in conditional statements

  • Helps control program flow

Array Data Type

An array is a collection of values stored under a single variable.

<?php
$fruits = array("Apple", "Banana", "Mango");

// Accessing array elements
echo "First fruit: " . $fruits[0] . "<br>";
echo "Second fruit: " . $fruits[1] . "<br>";

// Associative array
$ages = array("Vicky" => 25, "Sanjana" => 22);
echo "Vicky's age: " . $ages["Vicky"] . "<br>";
?>
  • Arrays can be indexed (numeric keys) or associative (custom keys)

  • Useful for storing multiple related values

Object Data Type

Objects are instances of classes and allow storing data and functions together.

<?php
class Person {
    public $name;
    public $age;

    function __construct($name, $age){
        $this->name = $name;
        $this->age = $age;
    }

    function greet() {
        echo "Hello, I am " . $this->name . " and I am " . $this->age . " years old.<br>";
    }
}

$person1 = new Person("Vicky", 25);
$person1->greet();
?>
  • Objects are used in object-oriented programming

  • Encapsulate data (properties) and behavior (methods)

NULL Data Type

NULL represents a variable with no value.

<?php
$emptyVar = NULL;
echo "Value: " . $emptyVar . "<br>"; // Outputs nothing
?>
  • Can be assigned explicitly or occur when a variable is declared but not assigned

  • Useful for resetting variables

Resource Data Type

Resources hold references to external resources like database connections or files.

<?php
// Example: opening a file creates a resource
$file = fopen("example.txt", "r");
var_dump($file); // Shows resource type
fclose($file);
?>
  • Resources are temporary and system-specific

  • Cannot be directly manipulated like strings or numbers

Checking Data Types

You can check a variable’s data type using the gettype() function.

<?php
$name = "Vicky";
$age = 25;
$isAvailable = true;

echo gettype($name) . "<br>"; // string
echo gettype($age) . "<br>"; // integer
echo gettype($isAvailable) . "<br>"; // boolean
?>
  • Useful for debugging or validating input

  • Can also use var_dump() to see both value and type

Summary of the Tutorial

  • PHP is loosely typed; data types are assigned automatically

  • Common data types: string, integer, float, boolean, array, object, NULL, resource

  • Strings can use single or double quotes

  • Arrays can be indexed or associative

  • Objects encapsulate properties and methods

  • Boolean values are used in conditions, and NULL represents no value

  • Resources reference external system objects like files or database connections

Mastering data types is essential because every variable, operation, and function relies on the correct type. Understanding them ensures your PHP code runs correctly and efficiently.


Practice Questions

  1. Create a PHP script that declares a string variable $name = "Vicky" and prints it using echo. Add a comment explaining that it is a string.

  2. Declare an integer variable $age = 25 and a float variable $height = 5.7. Print both using echo with comments describing each type.

  3. Create a boolean variable $isAvailable = true and use it in an if statement to print “Item available” if true, and “Item not available” if false. Include comments explaining the condition.

  4. Define an indexed array $fruits = array("Apple", "Banana", "Mango") and print the first and third element using echo. Add comments describing the array elements.

  5. Create an associative array $ages = array("Vicky" => 25, "Sanjana" => 22) and print each person’s age with comments explaining the key-value relationship.

  6. Write a PHP class Person with properties $name and $age, and a method greet() that prints a greeting. Instantiate an object and call the method. Include comments explaining each part.

  7. Declare a variable $emptyVar = NULL and print it. Add a comment explaining the purpose of the NULL type.

  8. Open a file named example.txt using fopen() and store it in a variable $file. Use var_dump() to show the resource type and include comments explaining what a resource is.

  9. Use the gettype() function to check the data type of $name, $age, and $isAvailable. Print the results and include comments explaining each type.

  10. Create a PHP script that declares a variable $price = 49.99, prints it using echo, and then prints its type using gettype(). Include comments explaining that it is a float.


Go Back Top