PHP Data Types

A data type defines the kind of value a variable can hold. PHP is a loosely typed language, which means it automatically assigns the appropriate data type when a value is stored in a variable. There is no need to specify the type manually. However, understanding data types is essential because different types behave differently in calculations, comparisons, and output.

PHP supports eight primitive data types, grouped into three categories: scalar types, compound types, and special types.

Scalar Data Types

Scalar types hold a single value. There are four scalar types in PHP.

String

A string is a sequence of characters — letters, numbers, symbols, or spaces — enclosed in either single or double quotes.

<?php
  $firstName = "Maria";
  $greeting = 'Good morning!';
  $sentence = "She said 'hello' to everyone.";

  echo $firstName;   // Outputs: Maria
  echo $greeting;    // Outputs: Good morning!
?>

Double-quoted strings allow variable interpolation — a variable inside a double-quoted string is replaced with its value. Single-quoted strings treat everything literally.

<?php
  $item = "laptop";

  echo "I bought a $item.";     // Outputs: I bought a laptop.
  echo 'I bought a $item.';     // Outputs: I bought a $item.
?>

Integer

An integer is a whole number — positive, negative, or zero — with no decimal point. PHP integers can be written in decimal, hexadecimal, octal, or binary format.

<?php
  $positiveNum = 100;
  $negativeNum = -45;
  $zero = 0;
  $hexValue = 0x1A;    // Hexadecimal (equals 26)
  $octalValue = 0755;  // Octal (equals 493)

  echo $positiveNum;   // Outputs: 100
  echo $negativeNum;   // Outputs: -45
  echo $hexValue;      // Outputs: 26
?>

Float (Double)

A float is a number with a decimal point, or a number written in exponential (scientific) notation. Floats are also called doubles or floating-point numbers.

<?php
  $temperature = 36.6;
  $price = 4.99;
  $scientific = 1.5e3;   // Equals 1500
  $tiny = 2.5E-4;        // Equals 0.00025

  echo $temperature;     // Outputs: 36.6
  echo $scientific;      // Outputs: 1500
?>

Boolean

A boolean holds one of two values: true or false. Booleans are commonly used in conditions and comparisons. When echoed, true prints as 1 and false prints as nothing (empty string).

<?php
  $isActive = true;
  $isAdmin = false;

  echo $isActive;   // Outputs: 1
  echo $isAdmin;    // Outputs: (nothing)

  if ($isActive) {
    echo "Account is active.";
  }
?>

Compound Data Types

Compound types hold multiple values or complex structures.

Array

An array stores multiple values in a single variable. Arrays are covered in depth in their own topic. A brief example:

<?php
  $colors = ["red", "green", "blue"];
  echo $colors[0];   // Outputs: red
  echo $colors[2];   // Outputs: blue
?>

Object

An object is an instance of a class. Objects are covered in the Object-Oriented Programming section of this course. They allow grouping of data and behavior together.

<?php
  class Car {
    public $brand = "Toyota";
  }

  $myCar = new Car();
  echo $myCar->brand;   // Outputs: Toyota
?>

Special Data Types

NULL

NULL is a special data type with only one possible value: null. A variable is NULL if it has been assigned NULL directly, or if it has been declared but not yet assigned a value.

<?php
  $emptyVar = null;
  $uninitializedVar;   // Also considered null

  echo is_null($emptyVar);  // Outputs: 1 (true)
?>

Resource

A resource is a special type that holds a reference to an external resource — such as an open file, a database connection, or a network socket. Resources are created and managed by PHP automatically through specific functions like fopen() or mysqli_connect().

Checking Data Types

PHP provides built-in functions to check the type of any variable.

gettype() Function

<?php
  $score = 98;
  $name = "Alex";
  $gpa = 3.75;
  $passed = true;
  $nothing = null;

  echo gettype($score);    // Outputs: integer
  echo gettype($name);     // Outputs: string
  echo gettype($gpa);      // Outputs: double
  echo gettype($passed);   // Outputs: boolean
  echo gettype($nothing);  // Outputs: NULL
?>

Type-Checking Functions

PHP also includes specific functions to confirm a variable's type:

<?php
  $value = 42;

  var_dump(is_int($value));     // bool(true)
  var_dump(is_string($value));  // bool(false)
  var_dump(is_numeric($value)); // bool(true)
?>

Type Juggling (Automatic Type Conversion)

PHP automatically converts types when needed. This is called type juggling.

<?php
  $number = "10";   // String
  $result = $number + 5;

  echo $result;           // Outputs: 15
  echo gettype($result);  // Outputs: integer
?>

PHP saw a numeric string and converted it to an integer automatically to perform the addition. This behavior is convenient but can cause unexpected results if not understood.

Type Casting

Type casting forces a value to become a specific type using a cast operator in parentheses.

<?php
  $floatNum = 7.9;
  $intNum = (int)$floatNum;    // Truncates decimal, does NOT round
  $strNum = (string)$floatNum;

  echo $intNum;   // Outputs: 7
  echo $strNum;   // Outputs: 7.9
  echo gettype($intNum);   // Outputs: integer
  echo gettype($strNum);   // Outputs: string
?>

Key Points

  • PHP has four scalar types: string, integer, float, and boolean.
  • Compound types include arrays and objects.
  • NULL is a special type representing the absence of a value.
  • PHP automatically determines a variable's type — no declaration needed.
  • Use gettype() or var_dump() to inspect a variable's type.
  • PHP performs automatic type conversion (type juggling) when needed.
  • Type casting forces a specific conversion using (int), (string), (float), etc.

Leave a Comment

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