PHP Data Types

Definition:
In PHP, a data type defines the kind of value a variable can store or process. Since PHP is a dynamically typed language, it automatically identifies the type of data during runtime, based on the assigned value.

Understanding PHP’s data types is essential for building reliable and bug-free programs.


Types of Data in PHP

PHP supports the following major data types:

  • String

  • Integer

  • Float (Double)

  • Boolean

  • Array

  • Object

  • NULL

  • Resource


php data types


String

Definition:
A string is a sequence of characters, such as "Hello World!". In PHP, you can use either single (') or double (") quotes.

Example:

<?php 
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>"; 
echo $y;
?>

Output:
Hello world!
Hello world!


Integer

Definition:
An integer is a whole number (without a decimal point), and it can be positive or negative.

Valid formats:

  • Decimal: 123

  • Hexadecimal: 0x1A

  • Octal: 0755

Rules for Integers:

  • Must have at least one digit

  • No decimal points allowed

  • Can be negative or positive

Example:

<?php  
$x = 5985;
var_dump($x);
?>

Output:
int(5985)


Float (or Double)

Definition:
A float is a number with a decimal point or in exponential form. It’s used for representing precise values like measurements or calculations.

Example:

<?php  
$x = 10.365;
var_dump($x);
?>

Output:
float(10.365)


Boolean

Definition:
A Boolean holds only two possible values: true or false. It’s commonly used in conditions and logical expressions.

Example:

<?php
$x = true;
$y = false;
?>

Note:
Booleans play a major role in decision-making using if, while, etc.


Array

Definition:
An array is a data type that stores multiple values in a single variable. You can access individual elements using their index.

Example:

<?php  
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>

Output:
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }


Object

Definition:
Objects are instances of classes. They bundle data (properties) and behavior (methods) together.

To use objects:

  1. Define a class using class

  2. Create an object using new

Example:

<?php
class Car {
    function Car() {
        $this->model = "VW";
    }
}

$herbie = new Car();
echo $herbie->model;
?>

Output:
VW


NULL

Definition:
NULL is a special data type that indicates a variable has no value. It’s automatically assigned when a variable is declared without a value.

Example:

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>

Output:
NULL


Resource

Definition:
A resource is not an actual data type but a special reference to external resources like database connections, file handles, or network streams.

Note:
Resources are used in advanced topics like MySQL, file I/O, etc., and will be covered later.


Checking Data Types in PHP

Useful Functions:

  • var_dump($x) – shows type and value

  • gettype($x) – returns the type as a string

  • is_int($x), is_string($x), is_bool($x), is_null($x), etc. – check specific types

Example:

<?php
$x = "Hello";
var_dump($x);  // string(5) "Hello"
?>

Conclusion

Understanding data types in PHP helps in organizing and processing information efficiently. By knowing what type of data you’re working with, you avoid bugs and write more optimized code. As you dive deeper into PHP, this knowledge becomes the foundation for handling variables, logic, and even databases with confidence.

Â