PHP Data Types

PHP supports several data types that can be used to store different kinds of values. Here are the commonly used data types in PHP:

PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers – also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

String:

Strings are sequences of characters enclosed in single quotes (') or double quotes ("). They can contain letters, numbers, symbols, and special characters. For example:

$name = "John Doe";
$message = 'Hello, World!';nte

Integer:

Integers are whole numbers without decimal points. They can be positive or negative. For example:

$age = 25;
$count = -10;

Float (Floating-Point Numbers):

Floats, also known as floating-point numbers or doubles, are numbers with decimal points. For example:

$pi = 3.14;
$price = 9.99;

Boolean:

Booleans represent the truth values of logic, either true or false. They are often used in conditional statements and comparisons. For example:

$isTrue = true;
$isFalse = false;

Array:

Arrays can store multiple values in a single variable. They can hold values of different data types and are indexed using numeric or associative keys. For example:

$numbers = [1, 2, 3, 4, 5];
$person = ['name' => 'John', 'age' => 25, 'city' => 'New York'];

Object:

Objects are instances of classes, which are used in object-oriented programming. Objects can have properties (variables) and methods (functions) associated with them. For example:

class Car {
    public $brand;
    public $color;
}

$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Blue";

NULL:

NULL is a special data type that represents the absence of a value. It is often used to indicate variables that have not been assigned a value. For example:

$var = null;

These are the basic data types in PHP. PHP also supports additional types like resource and callable, as well as more complex data structures such as multidimensional arrays and collections.