Sobes.tech
Intern

What data types exist in PHP?

sobes.tech AI

Answer from AI

PHP has eight data types:

Scalar types:

  • bool (boolean)
  • int (integer)
  • float (floating-point number)
  • string (string)

Compound types:

  • array (array)
  • object (object)

Special types:

  • resource (resource)
  • null (NULL)

Brief description of each type:

  • bool: Two possible values - true or false.
  • int: Whole numbers without a decimal part. The range depends on the platform.
  • float: Numbers with a decimal part or in exponential form.
  • string: A sequence of characters. Can be enclosed in single (') or double (") quotes.
  • array: An ordered map that can contain elements of any data type, accessible by numeric or string keys.
  • object: An instance of a class containing properties and methods.
  • resource: A special variable containing a reference to an external resource, such as a database or file.
  • null: A variable with no value. The only possible value of this type is NULL (case-insensitive).

Examples:

<?php

$is_active = true; // bool
$count = 10; // int
$price = 19.99; // float
$name = "John Doe"; // string
$colors = ["red", "green", "blue"]; // array
$user = new stdClass(); // object (example of an empty object)
$file_handle = fopen("path/to/file.txt", "r"); // resource
$variable_is_null = null; // null

?>
What data types exist in PHP? — PHP - sobes.tech