PHP is a loosely typed (dynamically typed) language, meaning you do not need to explicitly declare a variable’s data type when you create it. PHP automatically determines the type based on the value assigned.
PHP supports 10 primitive, compound, and special data types divided into three main categories:
These hold a single value.
bool): Represents truth values. It can only be true or false. Commonly used in conditional statements.int): Whole numbers without a decimal point (e.g., 42, -1950). Can be specified in decimal, hexadecimal, octal, or binary notation.float also called double or real): Numbers with fractional parts or scientific notation (e.g., 10.99, 3.14159, 2.4e3).string): A sequence of characters enclosed in single (') or double (") quotes (e.g., "Hello, Developer!").These can hold multiple values or complex structures.
array): An ordered map that can hold multiple values of any type. Can be indexed (numeric keys), associative (named keys), or multidimensional.object): An instance of a user-defined class containing properties (variables) and methods (functions).foreach construct.null): A special type representing a variable with no value. A variable is considered null if it has been assigned the constant NULL, has not been set yet, or has been unset.resource): A special variable holding a reference to an external external resource, such as a database connection, a file handle, or an image stream.You can check a variable’s type using built-in functions like gettype() or var_dump(), and convert types using type casting:
PHP
<?php
$score = "95.5"; // Currently a string
// Type casting string to float
$floatScore = (float) $score;
// Type casting float to integer
$intScore = (int) $floatScore; // Result: 95
var_dump($intScore); // Output: int(95)
?>
Would you like to explore PHP Arrays (indexed vs. associative arrays) in more detail next?
This website uses cookies.