0Pricing
PHP Academy · Lesson

PHP Data Types Overview

Explore strings, integers, floats, booleans, and NULL in PHP.

PHP Data Types Overview is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

PHP's Core Data Types

PHP supports four scalar types and several compound/special types:

  • Scalar: string, int, float, bool
  • Compound: array, object
  • Special: NULL, resource

PHP automatically chooses the type based on context — this is called type juggling.

Strings

Strings store text. Use single or double quotes:

<?php
$single = 'Hello World';     // single — no variable parsing
$name   = 'PHP';
$double = "Hello $name";     // double — variables are interpolated

echo $double; // Hello PHP
echo strlen($single); // 11

Integers and Floats

PHP integers are whole numbers; floats (doubles) hold decimal values:

<?php
$count  = 42;         // int
$neg    = -7;         // negative int
$hex    = 0x1A;       // hexadecimal = 26
$price  = 19.99;      // float
$sci    = 1.5e3;      // scientific = 1500.0

echo PHP_INT_MAX;   // largest integer on this platform
echo PHP_FLOAT_MAX; // largest float

Booleans

A boolean is either true or false. PHP evaluates many values as truthy or falsy:

  • Falsy: false, 0, 0.0, '', '0', [], NULL
  • Truthy: everything else
<?php
$isActive = true;
$isDone   = false;

if ($isActive) {
    echo 'Active';   // runs
}

var_dump((bool) '');   // bool(false)
var_dump((bool) 'hi'); // bool(true)

NULL

NULL represents the intentional absence of a value:

  • A variable is NULL if assigned NULL, never assigned, or unset
  • Use is_null($var) or === null to test for NULL
<?php
$nothing = null;

var_dump(is_null($nothing));   // bool(true)
var_dump($nothing === null);   // bool(true)
var_dump($nothing == false);   // bool(true) — loose comparison

Arrays as a Type

Arrays in PHP can hold mixed types and use integer or string keys:

<?php
$mixed = [42, 'hello', true, null];
$map   = ['name' => 'Alice', 'age' => 30];

echo gettype($mixed);        // array
echo count($mixed);          // 4
echo $map['name'];           // Alice

Type Functions

PHP provides is_* functions to check types at runtime:

<?php
$val = 3.14;

var_dump(is_int($val));    // bool(false)
var_dump(is_float($val));  // bool(true)
var_dump(is_string($val)); // bool(false)
var_dump(is_numeric('42')); // bool(true) — string that looks like a number

Strict Types Declaration

PHP 7+ allows strict type mode at the top of a file:

<?php
declare(strict_types=1);

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

echo add(3, 4);    // 7
// add(3, '4');   // TypeError in strict mode!

Type Hints in Functions

PHP 7+ supports type hints on parameters and return types to improve code clarity and catch bugs:

<?php
function greet(string $name): string {
    return 'Hello, ' . $name . '!';
}

function divide(float $a, float $b): ?float {
    if ($b === 0.0) return null;  // nullable return
    return $a / $b;
}

echo greet('PHP');          // Hello, PHP!
var_dump(divide(10, 0));    // NULL

Resources

A resource is a special variable holding a reference to an external resource like a file handle or database connection:

<?php
// fopen returns a resource
$handle = fopen('data.txt', 'r');

echo gettype($handle); // resource

fclose($handle); // always free resources when done

Union Types (PHP 8)

PHP 8 introduced union types, letting a parameter or return accept multiple types:

<?php
declare(strict_types=1);

function format(int|float $number): string {
    return number_format($number, 2);
}

echo format(1234);     // 1,234.00
echo format(9.5);      // 9.50

Quick Check

What does PHP's is_numeric() function return for the string '42'?

Recap: PHP Data Types

You've covered PHP's type system:

  • Scalar types: string, int, float, bool
  • NULL for intentional absence of value
  • Arrays and objects as compound types
  • Resources for external handles
  • PHP 8 union types for flexible signatures
  • Strict mode with declare(strict_types=1)

Next: outputting data to the browser with echo and print.

Frequently asked questions

Is the “PHP Data Types Overview” lesson free?

Yes — the full text of “PHP Data Types Overview” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “PHP Data Types Overview”?

Explore strings, integers, floats, booleans, and NULL in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “PHP Data Types Overview” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Variables in PHP
  2. PHP Data Types Overview
  3. Echo and Print Output
  4. Type Juggling and Type Casting
← Back to PHP Academy