Data Types

Loading concept...

PHP Data Types: The Building Blocks of Your Code đź§±

Imagine you’re organizing a toy box. You wouldn’t put your teddy bear in the same spot as your building blocks, right? Each toy has its own special place. PHP Data Types work the same way - they help PHP understand what kind of information you’re giving it!


What Are Data Types? 🤔

Think of data types like different containers in your kitchen:

  • A cookie jar holds cookies (text/words)
  • A measuring cup holds numbers
  • A light switch is either ON or OFF

PHP needs to know what “container” your data belongs to so it can work with it properly!

$name = "Luna";     // A word (String)
$age = 8;           // A number (Integer)
$isHappy = true;    // Yes or No (Boolean)

🎀 Strings: Words and Text

A String is any text wrapped in quotes. It’s like putting words inside a speech bubble!

What Can Be a String?

$greeting = "Hello, World!";
$number_text = "42";  // Still a string!
$emoji = "🎉";

Notice: Even numbers become strings when wrapped in quotes. "42" is text, not a number you can add.

Single vs Double Quotes

$name = "Luna";

// Double quotes: Variables work inside!
echo "Hi, $name!";  // Output: Hi, Luna!

// Single quotes: Shows exactly what you type
echo 'Hi, $name!';  // Output: Hi, $name!

Joining Strings Together (Concatenation)

Use the dot (.) to glue strings together:

$first = "Hello";
$second = "World";
$combined = $first . " " . $second;
// Result: "Hello World"

🔢 Integers and Floats: The Number Family

Integers: Whole Numbers

Integers are numbers without decimal points. Like counting on your fingers!

$apples = 5;
$temperature = -10;
$year = 2024;

Floats: Decimal Numbers

Floats (or doubles) have decimal points. Like measuring exactly how much juice is left!

$price = 9.99;
$pi = 3.14159;
$percentage = 75.5;

Quick Math Example

$a = 10;
$b = 3;

echo $a + $b;   // 13 (addition)
echo $a - $b;   // 7 (subtraction)
echo $a * $b;   // 30 (multiplication)
echo $a / $b;   // 3.333... (float!)

Magic Moment: When you divide two integers and get a decimal, PHP automatically gives you a float!


âś… Booleans: True or False

Booleans are like light switches - they can only be ON (true) or OFF (false).

$isRaining = true;
$hasHomework = false;

When Do We Use Booleans?

For making decisions!

$isLoggedIn = true;

if ($isLoggedIn) {
    echo "Welcome back!";
} else {
    echo "Please log in.";
}

Secret Truth About Booleans

Some values act like true or false even if they’re not:

Acts as FALSE Acts as TRUE
0 Any other number
"" (empty string) Any text
null Arrays with items
Empty array [] Most things!

🕳️ NULL: The “Nothing” Type

NULL means “I have nothing” or “I don’t know yet.”

Think of it like an empty envelope - the envelope exists, but there’s nothing inside.

$mystery = null;

NULL vs Empty String vs Zero

$nothing = null;   // No value at all
$empty = "";       // A string, just empty
$zero = 0;         // A number, the value 0

They’re all “empty-ish” but PHP treats them differently!

Checking for NULL

$box = null;

if (is_null($box)) {
    echo "The box is empty!";
}

📦 Arrays: Collections of Things

An Array is like a backpack that can hold many items at once!

Simple Array (Indexed)

$fruits = ["apple", "banana", "cherry"];

echo $fruits[0];  // apple
echo $fruits[1];  // banana
echo $fruits[2];  // cherry

Remember: PHP starts counting from 0, not 1!

Named Array (Associative)

Instead of numbers, use names as keys:

$pet = [
    "name" => "Fluffy",
    "type" => "cat",
    "age" => 3
];

echo $pet["name"];  // Fluffy
echo $pet["type"];  // cat

Adding to an Array

$colors = ["red", "blue"];
$colors[] = "green";  // Adds at the end

// Now: ["red", "blue", "green"]

Counting Items

$snacks = ["chips", "cookies", "fruit"];
echo count($snacks);  // 3

🎯 Quick Reference: Finding Data Types

Use gettype() to discover what type something is:

echo gettype("Hello");  // string
echo gettype(42);       // integer
echo gettype(3.14);     // double (float)
echo gettype(true);     // boolean
echo gettype(null);     // NULL
echo gettype([1,2,3]);  // array

🌟 The Big Picture

graph LR A[PHP Data Types] --> B[Strings] A --> C[Numbers] A --> D[Booleans] A --> E[NULL] A --> F[Arrays] B --> B1["Text in quotes"] C --> C1[Integers] C --> C2[Floats] C1 --> C1a["Whole numbers: 1, 42, -5"] C2 --> C2a["Decimals: 3.14, 9.99"] D --> D1["true or false"] E --> E1["Nothing/Unknown"] F --> F1["Collections of values"]

🎉 You Did It!

You now understand the 6 fundamental data types in PHP:

  1. Strings - Text and words
  2. Integers - Whole numbers
  3. Floats - Decimal numbers
  4. Booleans - True/False switches
  5. NULL - Empty/Unknown
  6. Arrays - Collections of data

Think of data types as speaking PHP’s language. When you tell PHP exactly what kind of data you have, it can help you do amazing things with it!

Next Adventure: Now that you know the building blocks, you’re ready to build incredible things! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.