In PHP, variables can hold different types of values or data. Here's a rundown of the primary data types:
Whole numbers, without a fractional part, within a certain range. They can be decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
$decimal = 42;
$hexadecimal = 0x2A;
$octal = 052;
$binary = 0b101010;
Floating-point numbers are numbers with a decimal point or in exponential form.
$float1 = 42.42;
$float2 = 4.2E1; // 4.2 * 10^1
Booleans are simple true/false values. They are often used in conditional testing.
$true = true;
$false = false;
Strings are sequences of characters, used for storing and manipulating text.
$string1 = 'Hello, world!';
$string2 = "Hello, world!";
Arrays can hold multiple values at once. Values are accessed by keys, which can be integers or strings.
$array1 = [1, 2, 3];
$array2 = ['one' => 1, 'two' => 2, 'three' => 3];
Objects are instances of classes and enable complex data structures and functions (methods) to operate on that data.
class MyClass {
public $property = 'Hello, world!';
}
$object = new MyClass();
Resources are special variables that hold references to external resources, like database connections or file handles. They are typically created by special functions and used in various native PHP functions.
$handle = fopen("file.txt", "r");
The NULL data type represents a variable that has no value assigned to it or has been explicitly set to null.
$var = null;
The Callable type indicates that a variable contains a reference to a function or method.
$callable = 'strlen'; // Reference to a built-in function
You can use various functions to check the type of a variable, such as is_int(), is_float(), is_string(), is_array(), is_object(), etc. You can also get the type as a string using gettype().
$type = gettype(42); // Returns 'integer'
Understanding these data types and how they interact is crucial for effective PHP programming, as each has its own set of behaviors, capabilities, and limitations.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here