Course Content
About Lesson

Data Types (string, integer, float, boolean, array, object, NULL)

  • In PHP, a data type defines the type of value a variable can hold.
  • PHP is loosely typed → You don’t need to declare the type of variable (like int, string) before using it. PHP automatically assigns a type depending on the value.
PHP
<?php
$x = 10;        // integer
$y = "Hello";   // string
$z = 10.5;      // float
?>
  • gettype($var) → returns type as string
  • You can check the type using:
  • var_dump($var) → shows type + value
  • A string is a sequence of characters inside single quotes (‘ ‘) or double quotes (” “).
  • Strings can contain letters, numbers, spaces, and special characters.
  • In double quotes, variables are parsed (expanded). In single quotes, they are not.
    PHP
    <?php
    $name = "Alice";
    $greeting1 = "Hello, $name";   // variable is expanded
    $greeting2 = 'Hello, $name';   // variable is not expanded
    
    echo $greeting1;  // Output: Hello, Alice
    echo "<br>";
    echo $greeting2;  // Output: Hello, $name
    ?>
    • An integer is a whole number (no decimal point).
    • It can be positive, negative, or zero.
    • Range depends on system (usually 64-bit).
    PHP
    <?php
    $a = 42;        // positive integer
    $b = -15;       // negative integer
    $c = 0;         // zero
    
    echo gettype($a); // Output: integer
    ?>
    • A float is a number with a decimal point or in exponential form.
    PHP
    <?php
    $pi = 3.14159;
    $price = 99.99;
    $exp = 1.2e3;  // 1200 (exponential notation)
    
    echo var_dump($pi);   // float(3.14159)
    echo "<br>";
    echo var_dump($exp);  // float(1200)
    ?>
    • A boolean has only two possible values:
      • true (1)
      • false (0)
    • Used for conditions and comparisons.
    PHP
    <?php
    $is_admin = true;
    $is_logged_in = false;
    
    echo var_dump($is_admin);      // bool(true)
    echo "<br>";
    echo var_dump($is_logged_in);  // bool(false)
    
    if($is_admin){
      echo "Welcome, Admin!";
    }
    ?>
    • An array stores multiple values in a single variable.
    • Types of arrays:
      1. Indexed Array → Numeric keys (0,1,2…)
      2. Associative Array → Named keys (like dictionary)
      3. Multidimensional Array → Array inside an array.
    PHP
    <?php
    // Indexed array
    $colors = ["red", "green", "blue"];
    
    // Associative array
    $student = ["name" => "John", "age" => 21, "grade" => "A"];
    
    // Multidimensional array
    $matrix = [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ];
    
    echo $colors[0];  // Output: red
    echo "<br>";
    echo $student["name"]; // Output: John
    echo "<br>";
    echo $matrix[1][2]; // Output: 6
    ?>
    • An object is an instance of a class.
    • A class is a blueprint, and objects are created from it.
    PHP
    <?php
    class Car {
      public $brand;
      public $color;
    
      function __construct($brand, $color){
        $this->brand = $brand;
        $this->color = $color;
      }
    
      function display(){
        return "Car: $this->brand, Color: $this->color";
      }
    }
    
    $car1 = new Car("Toyota", "Red");
    echo $car1->display();   // Output: Car: Toyota, Color: Red
    ?>
    • NULL is a special type that represents a variable with no value.
    • Used to reset a variable or mark “empty”.
    • NULL is different from an empty string ("") or zero (0).
    PHP
    <?php
    $x = "Hello";
    $x = null;
    
    var_dump($x); // NULL
    ?>
    • A resource is a special variable that holds a reference to an external resource (like database connection, file handle).
    • Example: MySQL connection, file opened with fopen().
    PHP
    <?php
    $file = fopen("test.txt", "r");
    var_dump($file);   // resource(type=stream)
    fclose($file);
    ?>