Data Types
Data Types
It refers to the type of value a variable has and what type of mathematical, relational, or
logical operations can be applied without causing an error.
IMPORTANCE OF DATA TYPES
Data types help programmers define the kind of data they are storing in a variable or element,
thus indicating the same to the program during execution.
MOST COMMONLY USED DATA TYPES
1. Int or Integer- A data type representing whole numbers.
Example:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration: $i\n";
}
?>
Common Uses:
Math operations (e.g., addition, subtraction, multiplication)
Loop counters (e.g., for loops)
Storing ages, quantities, and IDs
Indexing arrays/lists
2. Double- A data type that can represent any numerical value in the compiler, including
decimal values.
Example:
<?php
$grade1 = 85.5;
$grade2 = 90.2;
$average = ($grade1 + $grade2) / 2;
echo "Average Grade: $average\n";
?>
Common Uses:
Prices and money calculations
Scientific measurements (e.g., weight, temperature)
Averages and percentages
3. String- A data type representing a sequence of characters.
Example:
<?php
$name = "John";
echo "Welcome, $name!";
?>
Common Uses:
Usernames and names
Messages or notifications
Concatenating (joining) words together
4. Char or Character- A data type used for single letters.
Example:
<?php
$grade = 'A';
echo "Student's Grade: $grade\n";
?>
Common Uses:
Single-letter grades (A, B, C, D, F)
Gender representation ('M' for Male, 'F' for Female)
Shortcut codes (e.g., 'Y' for Yes, 'N' for No)