Current Location: Home> Latest Articles> Comprehensive Guide to PHP Basic Data Types and Type Conversion

Comprehensive Guide to PHP Basic Data Types and Type Conversion

M66 2025-10-16

PHP Basic Data Types

PHP is a loosely typed language, which means variables do not need explicit type declarations. However, PHP supports several basic data types, including integers, floats, strings, booleans, arrays, objects, and null.

Integer

Integers represent whole numbers, such as 10, -5, or 12345.

Float / Double

Floats represent numbers with decimal points, for example 3.14, -12.5, or 1.6e5.

String

Strings are sequences of characters, enclosed in single or double quotes, for example "Hello, world!", 'PHP', or "123".

Boolean

Booleans have two possible values: true and false, used for logical conditions.

Array

Arrays are used to store ordered collections of values, represented using square brackets, for example

$arr = [1, 2, 3];

Object

Objects represent instances of a class and are used to encapsulate properties and methods.

Null

Null represents an unset or non-existent value, usually written as

NULL
.

Type Conversion

PHP can automatically convert a variable from one type to another depending on context, for example:

  • Integer to float: "10" will automatically convert to 10.0
  • String to integer: "123" will automatically convert to 123
  • Float to string: 3.14 will automatically convert to "3.14"

Type Checking

You can use the gettype() function to check a variable's type. This function returns a string describing the variable's type, for example:

$x = 10;
echo gettype($x); // Output: integer