A data type defines the kind of data a variable can hold (e.g., text, whole numbers, or decimal numbers). Knowing the type is crucial because different types of data are treated differently when performing operations.
PHP is a loosely typed language, meaning you do not have to declare the data type of a variable; PHP automatically converts it based on the value assigned.
The 8 Primary PHP Data Types
PHP has eight primary data types. We will cover the six most common scalar (single value) types first:
| Type | Description | Example |
| String | A sequence of characters (text). Must be enclosed in single or double quotes. | $name = "Alice"; |
| Integer | A non-decimal number (a whole number). | $age = 35; |
| Float | A number with a decimal point or a number in exponential form. | $price = 19.99; |
| Boolean | Represents two possible states: TRUE or FALSE. Used for conditional testing. | $is_admin = true; |
| Array | Stores multiple values in a single variable. (Covered in a later chapter). | $cars = array("Volvo", "BMW"); |
| Object | Stores data and information on how to process that data. (Covered in OOP chapters). | new User(); |
| NULL | A variable that has no value assigned to it. | $var = null; |
| Resource | A special variable, holding a reference to an external resource (e.g., a database connection). | (Used internally by functions) |
1. String
A string is a series of characters. You can use either single quotes (') or double quotes (") around the string.
Example:
PHP
<?php
$text1 = 'Hello World in single quotes!';
$text2 = "Hello World in double quotes!";
// Concatenation (joining strings) uses the dot (.) operator
$greeting = $text1 . " - and - " . $text2;
echo $greeting;
?>
2. Integer and Float
Integers are whole numbers. Floats are numbers with decimal points. PHP handles standard math operations on these types.
Example:
PHP
<?php
$int_val = 500; // Integer
$float_val = 15.75; // Float
$sum = $int_val + $float_val;
echo "Sum is: " . $sum; // Output: Sum is: 515.75
?>
3. Boolean
Booleans are often the result of conditional expressions (e.g., Is $age greater than 18?). They are essential for controlling the flow of your program.
| Value | Output when echoed |
true | Prints the number 1 |
false | Prints nothing (empty string) |
Example:
PHP
<?php
$is_logged_in = true;
$is_subscribed = false;
// You can use a PHP function to check the type and value:
var_dump($is_logged_in);
// Output: bool(true)
?>
4. NULL
The NULL data type can only hold one value: NULL. A variable is NULL if:
- It has been explicitly assigned the value
NULL. - It has been created but has not been assigned a value.
Example:
PHP
<?php
$car = "Volvo";
$car = null; // $car is now NULL
var_dump($car);
// Output: NULL
?>
?>
PHP Type Juggling
Because PHP is loosely typed, it attempts to convert data types automatically when needed.
Example of Type Juggling:
PHP
<?php
$num_string = "10"; // A String
$num_int = 5; // An Integer
$result = $num_string + $num_int; // PHP converts "10" to the number 10
var_dump($result);
// Output: int(15)
?>
Next Steps
Now that you know the basic building blocks of data, we need to learn how to do meaningful work with them. The next chapter will focus on “PHP Operators”—the symbols used to perform operations on values and variables.
