Sobes.tech
Intern

What are the types of data in PHP?

sobes.tech AI

Answer from AI

In PHP, there are eight data types:

  1. Scalar types:

    • boolean: Logical value (true or false).
    • integer: Whole number.
    • float (or double): Number with a floating point.
    • string: Sequence of characters.
  2. Compound types:

    • array: Ordered set of values.
    • object: Instance of a class.
  3. Special types:

    • resource: Reference to an external resource (e.g., a file, database connection).
    • null: Absence of a value.

Examples:

<?php

// Boolean
$is_valid = true;

// Integer
$count = 123;

// Float
$price = 99.99;

// String
$message = "Hello, world!";

// Array
$colors = ["red", "green", "blue"];

// Object (example with anonymous class)
$user = new class {
    public $name = "Ivan";
};

// Resource (e.g., an open file)
// $file_handle = fopen("myfile.txt", "r");

// Null
$data = null;

?>
What are the types of data in PHP? — PHP - sobes.tech