The values assigned to a PHP variable can be of various data types, ranging from simple string and numeric types to more complex types like arrays and objects.
PHP supports a total of eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These data types are used to define variables. Now, let's explore each type in detail.
Integers are whole numbers without a decimal point (..., -2, -1, 0, 1, 2, ...). Integers can be expressed in decimal (base 10), hexadecimal (base 16 - prefixed with 0x
), or octal (base 8 - prefixed with 0
) notation, optionally preceded by a sign (-
or +
).
<?php
$a = 123; // decimal number
var_dump($a);
echo "<br>";
$b = -123; // a negative number
var_dump($b);
echo "<br>";
$c = 0x1A; // hexadecimal number
var_dump($c);
echo "<br>";
$d = 0123; // octal number
var_dump($d);
?>
Note: Starting from PHP 5.4, you can specify integers in binary (base 2) notation as well. To use binary notation, prefix the number with 0b (e.g., $var = 0b11111111;
).
Strings are sequences of characters, where each character is represented as a byte.
A string can contain letters, numbers, and special characters, and it can be as large as up to 2GB (2147483647 bytes maximum). The easiest way to define a string is by enclosing it in single quotes (e.g., 'Hello world!'), although you can also use double quotes ("Hello world!").
<?php
$a = 'Hello world!';
echo $a;
echo "<br>";
$b = "Hello world!";
echo $b;
echo "<br>";
$c = 'Stay here, I\'ll be back.';
echo $c;
?>
You can explore more about strings in the PHP Strings tutorial.
Floating point numbers (also known as "floats", "doubles", or "real numbers") are decimal or fractional numbers, as shown in the example below.
<?php
$a = 1.234;
var_dump($a);
echo "<br>";
$b = 10.2e3;
var_dump($b);
echo "<br>";
$c = 4E-10;
var_dump($c);
?>
Booleans act like a switch with only two possible values: 1
(true) or 0
(false).
<?php
// Assign the value TRUE to a variable
$show_error = true;
var_dump($show_error);
?>
An array is a variable that can store multiple values simultaneously. It's useful for grouping together related items, such as a list of country or city names.
An array is formally defined as an indexed collection of data values. Each index (also known as a key) in an array is unique and points to a corresponding value.
<?php
$colors = array("Red", "Green", "Blue");
var_dump($colors);
echo "<br>";
$color_codes = array(
"Red" => "#ff0000",
"Green" => "#00ff00",
"Blue" => "#0000ff"
);
var_dump($color_codes);
?>
To learn more about arrays, check out the PHP Array tutorial.
In PHP, an object is a data type that not only holds data but also includes instructions on how to use that data. Objects are instances of classes, which serve as blueprints for creating objects. Objects are created using the new
keyword.
Each object has its own unique set of properties (attributes) and methods (functions) inherited from its class. Each instance of an object operates independently, allowing it to be manipulated separately from other instances of the same class.
Here's a simple example illustrating how a class is defined and an object is created:
<?php
// Class definition
class greeting{
// properties
public $str = "Hello World!";
// methods
function show_greeting(){
return $this->str;
}
}
// Create object from class
$message = new greeting;
var_dump($message);
?>
Tip: In object-oriented programming, the data stored within an object are called its properties, while the instructions or code that operate on that data are known as the methods of the object.
The special NULL value in PHP is used to denote variables that do not contain any data. A variable assigned with NULL essentially means it has no value or has been explicitly reset to null.
<?php
$a = NULL;
var_dump($a);
echo "<br>";
$b = "Hello World!";
$b = NULL;
var_dump($b);
?>
When a variable is created without a value in PHP, like $var;
, it is automatically assigned a value of null. Many new PHP developers mistakenly consider $var1 = NULL;
and $var2 = "";
to be the same, but they are different. $var1
has a null value, while $var2
indicates that no value has been assigned to it.
A resource in PHP is a special variable that holds a reference to an external resource, such as an open file or a database connection.
<?php
// Open a file for reading
$handle = fopen("note.txt", "r");
var_dump($handle);
echo "<br>";
// Connect to MySQL database server with default setting
$link = mysqli_connect("localhost", "root", "");
var_dump($link);
?>