PHP Data Types

Understanding different types of data in PHP

🗂️ What are Data Types?

Data types specify what kind of data a variable can hold. PHP supports various data types including strings, integers, floats, booleans, arrays, objects, and more for different programming needs.


<?php
$text = "Hello";      // String
$number = 42;         // Integer
$price = 19.99;       // Float
$isActive = true;     // Boolean
?>
                                    

Output:

String: Hello

Integer: 42

Float: 19.99

Boolean: true

PHP Data Types

🔤

String

A sequence of characters enclosed in quotes. Strings can contain letters, numbers, and special characters for text data.

<?php
$name = "John Doe";
$message = 'Hello World';
?>
🔢

Integer

Whole numbers without decimal points. Integers can be positive, negative, or zero and are used for counting and calculations.

<?php
$age = 25;
$year = 2024;
?>
💰

Float

Numbers with decimal points, also called doubles. Floats are used for precise calculations involving fractions and monetary values.

<?php
$price = 99.99;
$pi = 3.14159;
?>

Boolean

Represents true or false values. Booleans are essential for conditional logic and decision-making in programs.

<?php
$isLoggedIn = true;
$hasAccess = false;
?>
📋

Array

Stores multiple values in a single variable. Arrays can hold different data types and are accessed using numeric or string keys.

<?php
$colors = ["red", "blue"];
$person = ["name"=>"John"];
?>
🎯

Object

Instances of classes that contain properties and methods. Objects enable object-oriented programming for complex data structures.

<?php
class Car {
  public $color = "red";
}
?>

NULL

Represents a variable with no value assigned. NULL is useful for indicating missing or undefined data in your programs.

<?php
$empty = NULL;
$notSet = null;
?>
📦

Resource

Special variables holding references to external resources like database connections or file handles for system operations.

<?php
$file = fopen("data.txt", "r");
// Resource type
?>

🔹 String Data Type

Strings are sequences of characters:

<?php
// Single quotes
$name = 'Alice';

// Double quotes
$greeting = "Hello World";

// Multi-line string
$address = "123 Main Street
New York, NY 10001";

echo "Name: $name<br>";
echo "Greeting: $greeting<br>";
echo "Address: $address";
?>

Output:

Name: Alice

Greeting: Hello World

Address: 123 Main Street
New York, NY 10001

🔹 Integer and Float

Numeric data types for calculations:

<?php
// Integer
$age = 30;
$year = 2024;
$negative = -15;

// Float
$price = 29.99;
$temperature = 98.6;
$pi = 3.14159;

echo "Age: $age<br>";
echo "Price: $price<br>";
echo "Pi: $pi";
?>

Output:

Age: 30

Price: 29.99

Pi: 3.14159

🔹 Boolean Data Type

True or false values for logic:

<?php
$isActive = true;
$hasPermission = false;

echo "Is Active: " . ($isActive ? "Yes" : "No") . "<br>";
echo "Has Permission: " . ($hasPermission ? "Yes" : "No");

// Boolean in conditions
if ($isActive) {
    echo "<br>User is active!";
}
?>

Output:

Is Active: Yes

Has Permission: No

User is active!

🔹 Array Data Type

Store multiple values in one variable:

<?php
// Indexed array
$fruits = ["Apple", "Banana", "Orange"];

// Associative array
$person = [
    "name" => "John",
    "age" => 25,
    "city" => "Boston"
];

echo "First fruit: " . $fruits[0] . "<br>";
echo "Name: " . $person["name"] . "<br>";
echo "Age: " . $person["age"];
?>

Output:

First fruit: Apple

Name: John

Age: 25

🔹 Checking Data Types

Use var_dump() to check variable types:

<?php
$name = "Alice";
$age = 25;
$price = 19.99;
$isActive = true;

echo "String: ";
var_dump($name);
echo "<br>Integer: ";
var_dump($age);
echo "<br>Float: ";
var_dump($price);
echo "<br>Boolean: ";
var_dump($isActive);
?>

Output:

String: string(5) "Alice"

Integer: int(25)

Float: float(19.99)

Boolean: bool(true)

🧠 Test Your Knowledge

Which data type is used for decimal numbers?