PHP Data Types
PHP has several data types that you can use to store and manipulate data. Here’s a quick rundown with examples:
1. Integers
- Description: Whole numbers, positive or negative, without decimal points.
- Example:
$age = 25; echo $age; // Output: 25
2. Floats (Floating Point Numbers)
- Description: Numbers with decimal points.
- Example:
$price = 19.99; echo $price; // Output: 19.99
3. Strings
- Description: Sequences of characters enclosed in quotes (single or double).
- Example:
$name = "John Doe"; echo $name; // Output: John Doe
4. Booleans
- Description: Represents truth values,
true
orfalse
. - Example:
$is_logged_in = true; if ($is_logged_in) { echo "Welcome back!"; // Output: Welcome back! }
5. Arrays
- Description: Collections of values indexed by keys (can be numbers or strings).
- Example:
$fruits = array("apple", "banana", "cherry"); echo $fruits[1]; // Output: banana
6. Objects
- Description: Instances of classes, used in object-oriented programming.
- Example:
class Car { public $color; function __construct($color) { $this->color = $color; } } $myCar = new Car("red"); echo $myCar->color; // Output: red
7. NULL
- Description: Represents a variable with no value.
- Example:
$emptyVar = null; if (is_null($emptyVar)) { echo "The variable is null."; // Output: The variable is null. }
8. Resources
- Description: Special variables that hold references to external resources (e.g., database connections).
- Example:
$conn = mysqli_connect("localhost", "username", "password", "database"); if ($conn) { echo "Database connection established."; // Output: Database connection established. }
Each data type in PHP has its specific use case and behaves differently in operations and functions.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.