PHP data types
In PHP, data types are used to classify and manage different types of data within a script. Understanding data types is crucial for effectively working with variables, as it allows you to perform appropriate operations on the data. PHP supports several data types, and they can broadly be categorized into scalar types, compound types, and special types.
1. Scalar Types
Scalar types represent a single value.
a) Integer
Definition: Integers are whole numbers without any decimal point.
Syntax:
$intVar = 42;
Range: The range of integers depends on the platform, typically from -2^31 to 2^31-1 on 32-bit systems.
Examples:
<?php $positiveInt = 123; $negativeInt = -456; $hexadecimal = 0x1A; // Hexadecimal (26 in decimal) $octal = 0755; // Octal (493 in decimal) ?>
b) Float (Double)
- Definition: Floats, also known as doubles, are numbers with a decimal point or in exponential form.
- Syntax:
$floatVar = 3.14;
- Examples:
<?php $floatNumber = 3.14159; $scientificNotation = 2.5e3; // 2.5 * 10^3 = 2500 ?>
c) String
Definition: Strings are sequences of characters enclosed in single or double quotes.
Syntax:
$stringVar = "Hello, World!";
Examples:
<?php $simpleString = "Hello, World!"; $singleQuoteString = 'Single quoted string'; $interpolatedString = "The value of pi is $floatNumber"; ?>
String Interpolation: When using double quotes, PHP interprets variables within the string.
<?php $name = "John"; echo "Hello, $name!"; // Outputs: Hello, John! ?>
d) Boolean
- Definition: Booleans represent two possible states:
true
orfalse
. - Syntax:
$boolVar = true;
- Examples:
<?php $isTrue = true; $isFalse = false; ?>
2. Compound Types
Compound types can hold multiple values.
a) Array
- Definition: An array is a collection of values, which can be indexed numerically or associatively.
- Syntax:
$arrayVar = array("value1", "value2", "value3");
- Types of Arrays:
- Indexed Arrays:
<?php $colors = array("red", "green", "blue"); echo $colors[0]; // Outputs: red ?>
- Associative Arrays:
<?php $age = array("John" => 25, "Jane" => 30); echo $age["John"]; // Outputs: 25 ?>
- Multidimensional Arrays:
<?php $matrix = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) ); echo $matrix[1][2]; // Outputs: 6 ?>
- Indexed Arrays:
b) Object
Definition: Objects are instances of classes, which can contain both data (properties) and functions (methods).
Syntax:
$objectVar = new ClassName();
Example:
<?php class Car { public $model; public $color; public function __construct($model, $color) { $this->model = $model; $this->color = $color; } public function start() { return "The car has started"; } } $myCar = new Car("Toyota", "Red"); echo $myCar->model; // Outputs: Toyota echo $myCar->start(); // Outputs: The car has started ?>
3. Special Types
Special types are used for specific purposes.
a) NULL
- Definition: NULL represents a variable with no value. It is the only possible value of the special type
NULL
. - Syntax:
$nullVar = NULL;
- Example:
<?php $var = NULL; if (is_null($var)) { echo "The variable is NULL"; } ?>
b) Resource
- Definition: Resources are special variables that hold references to external resources, such as database connections, file handles, etc.
- Example:
<?php $file = fopen("example.txt", "r"); // $file is a resource // Use the resource... fclose($file); // Always close the resource when done ?>
4. Type Casting
PHP allows you to explicitly convert a variable from one type to another.
Syntax:
$var = (int)$var; // Casts $var to an integer
Example:
<?php $var = "3.14"; $intVar = (int)$var; // $intVar is now 3 ?>
5. Type Juggling
PHP automatically converts variables to the appropriate type based on the context.
- Example:
<?php $sum = "3" + 2; // PHP converts "3" to an integer, and $sum is 5 $result = 5 . " apples"; // PHP converts 5 to a string, and $result is "5 apples" ?>