PHP Data Types

In PHP, data types tell the computer what kind of value a variable can hold. For example, a number is stored differently than text. Understanding data types is important because it helps you use variables correctly in your program.

1. String

A string is a sequence of characters, like text. Strings are always written inside quotes.

Example:
<?php
    $name = "John Doe";
    echo "Hello, my name is $name";
?>
  • "John Doe" is a string value.
  • Output will be: Hello, my name is John Doe.

2. Integer (int)

An integer is a whole number without decimals.

Example:
<?php
    $age = 25;
    echo "I am $age years old.";
?>
  • 25 is an integer.
  • Output will be: I am 25 years old.

3. Float (Decimal Number)

A float is a number with a decimal point.

Example:
<?php
    $price = 49.99;
    echo "The price is $price dollars.";
?>
  • 49.99 is a float value.
  • Output will be: The price is 49.99 dollars.

4. Boolean

A boolean has only two possible values: true or false. It is often used for conditions.

Example:
<?php
    $is_student = true;
    if($is_student){
        echo "You are a student.";
    } else {
        echo "You are not a student.";
    }
?>
  • true means β€œyes” or β€œon”.
  • false means β€œno” or β€œoff”.
  • In this example, output will be: You are a student.

5. Array

An array stores multiple values in a single variable.

Example:
<?php
    $colors = array("Red", "Green", "Blue");
    echo $colors[0]; // Outputs "Red"
?>
  • Arrays hold multiple values (list of items).
  • $colors[0] gives the first item (Red).

6. NULL

NULL is a special type that means "no value" or "empty".

Example:
<?php
    $value = null;
    var_dump($value); // Shows NULL
?>
  • NULL means the variable has no value assigned.
  • Useful when you want to clear a variable or reset it.

βœ… Summary: PHP supports different data types like string, integer, float, boolean, array, and null. Choosing the right data type makes your program more reliable.

PHP Tutorial