PHP Data Types
A data type specifies the type of data a variable can hold. It helps identify what kind of value is stored in a variable or what type of value it will hold in the future.
Key Points
- PHP is a flexible language compared to languages like Java or C, where you have to define the data type of a variable when declaring it.
- In PHP, the type of a variable is not explicitly defined by the programmer; it is determined at runtime based on the assigned value.
PHP Primitive Data Types
PHP supports 10 primitive data types, divided into scalar, compound, and special types.
Scalar Types:
- Boolean: Represents true or false values.
- Integer: Represents whole numbers like
1
,2
,3
, etc. - Float: Represents numbers with decimal points like
1.3
,0.9
, etc. - String: Represents text enclosed in single (‘ ‘) or double (” “) quotes.
Compound Types:
- Array: Represents a collection of values. Can be indexed, associative, or multidimensional.
- Object: Represents instances of classes. Example:
$x = new ClassObj();
- Callable: Refers to functions that can be called.
- Iterable: Represents an array or any object that can be traversed.
Special Types:
- Resource: Holds references to external resources like files, databases, etc.
- NULL: Represents a variable with no value.
Type Checking Functions
To check the type of a variable, you can use:
var_dump()
: Provides detailed information about the variable’s type and value.gettype()
: Returns the type of the variable as a string.
Boolean Data Type
Boolean can only hold two values: true
or false
. When printed, true
outputs 1
, while false
outputs nothing.
<?php
// Define Boolean values
$a_bool = true;
$b_bool = false;
echo "Boolean Value For True: " . $a_bool . "<br>";
echo "Boolean Value For False: " . $b_bool . "<br>";
?>
Output:
Boolean Value For True: 1
Boolean Value For False:
Integer Data Type
Integers can be defined as whole numbers, including negative values. They can also be represented in:
- Decimal (base 10): e.g.,
1234
- Octal (base 8): Prefixed with
0
, e.g.,0123
- Hexadecimal (base 16): Prefixed with
0x
, e.g.,0x1A
- Binary (base 2): Prefixed with
0b
, e.g.,0b11111111
<?php
// Different integer representations
$dec = 1234;
$oct = 0123;
$hex = 0x1A;
$bin = 0b11111111;
// Printing values with their types
echo "Hexadecimal Value: " . $hex . " And Type: " . gettype($hex) . "<br>";
echo "Decimal Value: " . $dec . " And Type: " . gettype($dec) . "<br>";
echo "Octal Value: " . $oct . " And Type: " . gettype($oct) . "<br>";
echo "Binary Value: " . $bin . " And Type: " . gettype($bin) . "<br>";
?>
Output:
Hexadecimal Value: 26 And Type: integer
Decimal Value: 1234 And Type: integer
Octal Value: 83 And Type: integer
Binary Value: 255 And Type: integer
Float Data Type
Floats, also known as floating-point numbers, represent real numbers with decimal points, such as 1.3
or 0.9
.
Array Data Type
In PHP, arrays are ordered maps that store data in key-value pairs. They can be indexed or associative.
Object Data Type
Objects are instances of classes, which are blueprints defining the properties and behavior of real-world entities.
String Data Type
Strings are sequences of characters. PHP allows strings to be defined in four ways:
- Single Quoted Strings:
'Hello World'
- Double Quoted Strings:
"Hello World"
- Heredoc Syntax: Used for defining multi-line strings without concatenation.
- Nowdoc Syntax: Similar to heredoc but behaves like single-quoted strings.
Understanding PHP data types is crucial for writing efficient and error-free code. Knowing how to handle different types helps in managing data and controlling the flow of your applications.