php data types
php data types
Sure, here are the definitions and examples of PHP data types:
1. Integers:
Definition: Integers are whole numbers without any decimal points. They can be posi
tive or negative.
Example:
Php Copy
$age = 25;
echo $age; // Output: 25
2. Floats (Doubles):
Definition: Floats are numbers with a decimal point.
Example:
Php Copy
$price = 9.99;
echo $price; // Output: 9.99
3. Strings:
Definition: Strings are sequences of characters enclosed in single or double quotes.
Example:
Php Copy
4. Booleans:
Definition: Booleans represent two possible states: TRUE or FALSE .
Example:
Php Copy
$is_admin = true;
if ($is_admin) {
echo "User is an admin.";
} else {
echo "User is not an admin.";
}
// Output: User is an admin.
5. Arrays:
Definition: Arrays are collections of values indexed by keys. They can be indexed or a
ssociative.
Examples:
Indexed Array:
Php Copy
Associative Array:
Php Copy
$person = array("name" => "John", "age" => 30, "city" => "New York");
echo $person["name"]; // Output: John
6. Objects:
Definition: Objects are instances of classes that can contain properties and methods.
Example:
Php Copy
class Car {
public $make;
public $model;
7. NULL:
Definition: NULL represents a variable with no value.
Example:
Php Copy
$var = NULL;
if (is_null($var)) {
echo "The variable is NULL.";
}
// Output: The variable is NULL.
8. Resources:
Definition: Resources are special variables holding references to external resources li
ke database connections or file handles.
Example:
Php Copy
9. Callable:
Definition: Callable represents a value that can be called as a function, such as a func
tion name, class method, or anonymous function.
Example:
Php Copy
function greet() {
return "Hello!";
}
$callback = "greet";
echo $callback(); // Output: Hello!
10. Iterable:
Definition: Iterable represents any value that can be looped through with a foreach
loop.
Example:
Php Copy
function getIterable() {
return array(1, 2, 3);
}
These definitions and examples should give you a solid understanding of PHP data types. If y
ou have any further questions or need more examples, feel free to ask!