Intern
What are the types of data in PHP?
sobes.tech AI
Answer from AI
In PHP, there are eight data types:
-
Scalar types:
boolean: Logical value (trueorfalse).integer: Whole number.float(ordouble): Number with a floating point.string: Sequence of characters.
-
Compound types:
array: Ordered set of values.object: Instance of a class.
-
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;
?>