PHP supports various data types such as integer, float, string, boolean, array, object, resource, & null that allow you to work with different kinds of values.
Data Types | Examples |
Integer | $age = 25 |
Float | $price = 12.99 |
String | $name = “John” |
Boolean | $yes = true, $no = false |
Array | $colors= [“red”, “blue”, “black”] |
Object | $car = new Car() |
Resource | $file = fopen(“example.txt”, “r”); |
Null | $x = null |
Integer Data Type
In PHP, the integer data type is used to represent whole numbers without any decimal points. It can be both positive and negative, and they can range from -2,147,483,648 to 2,147,483,647.
$count = 10; $temperature = -5; $year = 2022;
n the examples above, $count
, $temperature
, and $year
are variables of the integer data type. They store whole numbers representing quantities, temperatures, and years, respectively.
It’s important to note that PHP automatically determines the appropriate size for integers based on the platform. In most cases, integers are 32 bits on 32-bit systems and 64 bits on 64-bit systems. The PHP_INT_MAX
constant provides the maximum integer value that can be represented on the current platform.
echo PHP_INT_MAX;
Float Data Type
In PHP, the float data type is used to represent numbers that have decimal points or are written in exponential form.
Floats are also known as floating-point numbers. They allow you to work with values that require precision beyond whole numbers.
Floats are useful for representing measurements, calculations involving fractions, and other situations where the precision of decimal numbers is required.
$price = 9.99; $pi = 3.14159; $scientificNotation = 1.2e3; // Equivalent to 1200
In the examples above, $price
, $pi
, and $scientificNotation
are variables of the float data type. They store values with decimal points or in exponential notation.
String Data Type
In PHP, the string data type is used to represent a sequence of characters, such as text or numbers. Strings are versatile and can include letters, numbers, symbols, and spaces. They are enclosed in either single ('
) or double ("
) quotes.
$name = "Alice"; $message = 'This is a PHP string.'; $numberAsString = "42"; // Even though it looks like a number, it's treated as a string
n the examples above, $name
, $message
, and $numberAsString
are variables of the string data type. They store sequences of characters.
Boolean Data Type
In PHP, the boolean data type is used to represent values that can be either true or false. Booleans are often used in logical expressions and conditional statements to make decisions in a program.
$isTrue = true; $isFalse = false;
In the examples above, $isTrue
and $isFalse
are variables of the boolean data type. They can only hold the values true or false.
Array Data Type
In PHP, the array data type is used to represent an ordered, indexed collection of values. It allows you to store multiple values under a single variable name, making it convenient to work with sets of related data.
// Indexed Array $colors = ["red", "green", "blue"]; // Associative Array $person = [ 'name' => 'John', 'age' => 25, 'city' => 'New York' ];
Object Data Type
In PHP, the object data type is used to represent instances of user-defined classes. Objects allow you to bundle data (attributes) and functions (methods) related to a specific entity into a single unit.
class Car { public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . ", Year: " . $this->year; } } $myCar = new Car(); $myCar->model = "Toyota"; $myCar->year = 2022; $myCar->displayInfo();
In the example above, a class Car
is defined with two properties (model
and year
) and a method (displayInfo
). An object $myCar
is created based on this class, and its properties are set with specific values. The displayInfo
method is then called on the object, displaying information about the car.
Resource Data Type
In PHP, the resource data type is used to represent a special variable that holds a reference to an external resource, such as a file, database connection, or other system-level resource.
Resources are typically created and managed by functions outside of PHP, and they allow PHP scripts to interact with external entities
$fileHandle = fopen("example.txt", "r"); if (is_resource($fileHandle)) { echo "File opened successfully!"; } fclose($fileHandle);
In this example, the fopen()
function is used to open a file for reading. The result, $fileHandle
, is a resource variable that holds a reference to the opened file. The is_resource()
function is then used to check if the variable is a resource before proceeding with operations.
Other examples of resources include database connections (mysqli_connect()
), image handles (imagecreatefromjpeg()
), and file pointers (fopen()
).
Null Data Type
In PHP, the null
data type is a special value used to indicate that a variable has no value or does not contain any data.
It is often used to initialize variables before assigning them a specific value or to explicitly represent the absence of a value.
$name = null; if ($name === null) { echo "The variable is null."; } else { echo "The variable has a value."; }
In this example, the variable $name
is explicitly assigned the value null
. The ===
operator is then used to check if the variable is equal to null
. If the variable is null
, it will execute the corresponding message indicating that the variable is null.