PHP supports various data types to represent different kinds of values in your code. Understanding data types is crucial for writing correct and efficient programs. In this section, we'll explore the fundamental data types in PHP.

Scalar Types

Scalar types in PHP represent single values. There are four scalar types:

1. Boolean

The boolean data type represents a logical value, either `true` or `false`. Booleans are commonly used in conditional statements and loops.

 

$isActive = true;
$isLoggedIn = false;

2. Integer

The integer data type represents whole numbers (without decimal parts), both positive and negative. Integers can be specified in decimal (base 10), hexadecimal (base 16, prefixed with `0x`), or octal (base 8, prefixed with `0`) notation.

 

$age = 25;            // Decimal
$hexadecimal = 0xA3;  // Hexadecimal
$octal = 0123;        // Octal



3. Float

The float data type represents floating-point numbers or numbers with decimal parts. Floats can also be expressed in scientific notation using `e` or `E`.

 

$price = 19.99;
$pi = 3.14159;
$sciNotation = 2.5e3;  // 2.5 x 10^3 = 2500



4. String

The string data type represents a sequence of characters. Strings can be enclosed in single quotes (`'`), double quotes (`"`), or heredoc syntax (`<<<`).

$name = 'Alice';
$message = "Hello, World!";
$multiline = <<<EOT
This is a
multi-line
string.
EOT;

Compound Types

Compound types in PHP represent collections of values. There are two compound types:

1. Array

An array is a collection of values, indexed numerically or associatively. Arrays can store values of different data types.

// Numeric array
$numbers = [1, 2, 3, 4, 5];

// Associative array
$person = [
    'name' => 'Alice',
    'age' => 25,
    'city' => 'New York'
];

2. Object

An object is an instance of a class, which is a blueprint for creating objects. Objects are used in object-oriented programming and will be covered in more detail later in the tutorial.

class Person {
    public $name;
    public $age;

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

$alice = new Person('Alice', 25);

Special Types

PHP also has two special types: `null` and `resource`.

1. Null

The `null` value represents a non-existent or invalid value. It is the only possible value of the `null` data type.

$name = null;

2. Resource

The `resource` data type represents external resources, such as database connections, file handles, or image streams. Resources are typically created by special functions and cannot be directly manipulated.

$file = fopen('file.txt', 'r');  // $file is a resource

Type Checking and Casting

PHP provides functions to check the data type of a value (`gettype()`, `is_*()`) and to convert a value from one type to another (`settype()`, type casting).

$age = 25;
$ageString = (string) $age;  // Type casting to string

echo gettype($age);      // Outputs: integer
echo gettype($ageString);// Outputs: string

In the next section, we'll explore operators in PHP, which allow you to perform various operations on values and variables.