About Lesson
Data Types (string, integer, float, boolean, array, object, NULL)
- In PHP, a data type defines the type of value a variable can hold.
- PHP is loosely typed → You don’t need to declare the type of variable (like
int
,string
) before using it. PHP automatically assigns a type depending on the value.
Example:
PHP
<?php
$x = 10; // integer
$y = "Hello"; // string
$z = 10.5; // float
?>
gettype($var)
→ returns type as string- You can check the type using:
var_dump($var)
→ shows type + value
1. String:
- A string is a sequence of characters inside single quotes (‘ ‘) or double quotes (” “).
- Strings can contain letters, numbers, spaces, and special characters.
- In double quotes, variables are parsed (expanded). In single quotes, they are not.
Example:
PHP
<?php
$name = "Alice";
$greeting1 = "Hello, $name"; // variable is expanded
$greeting2 = 'Hello, $name'; // variable is not expanded
echo $greeting1; // Output: Hello, Alice
echo "<br>";
echo $greeting2; // Output: Hello, $name
?>
2. Integer:
- An integer is a whole number (no decimal point).
- It can be positive, negative, or zero.
- Range depends on system (usually 64-bit).
Example:
PHP
<?php
$a = 42; // positive integer
$b = -15; // negative integer
$c = 0; // zero
echo gettype($a); // Output: integer
?>
3. Float (Double / Real Numbers):
- A float is a number with a decimal point or in exponential form.
Example:
PHP
<?php
$pi = 3.14159;
$price = 99.99;
$exp = 1.2e3; // 1200 (exponential notation)
echo var_dump($pi); // float(3.14159)
echo "<br>";
echo var_dump($exp); // float(1200)
?>
4. Boolean:
- A boolean has only two possible values:
true
(1)false
(0)
- Used for conditions and comparisons.
Example:
PHP
<?php
$is_admin = true;
$is_logged_in = false;
echo var_dump($is_admin); // bool(true)
echo "<br>";
echo var_dump($is_logged_in); // bool(false)
if($is_admin){
echo "Welcome, Admin!";
}
?>
5. Array:
- An array stores multiple values in a single variable.
- Types of arrays:
- Indexed Array → Numeric keys (0,1,2…)
- Associative Array → Named keys (like dictionary)
- Multidimensional Array → Array inside an array.
Example:
PHP
<?php
// Indexed array
$colors = ["red", "green", "blue"];
// Associative array
$student = ["name" => "John", "age" => 21, "grade" => "A"];
// Multidimensional array
$matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
];
echo $colors[0]; // Output: red
echo "<br>";
echo $student["name"]; // Output: John
echo "<br>";
echo $matrix[1][2]; // Output: 6
?>
6. Object:
- An object is an instance of a class.
- A class is a blueprint, and objects are created from it.
Example:
PHP
<?php
class Car {
public $brand;
public $color;
function __construct($brand, $color){
$this->brand = $brand;
$this->color = $color;
}
function display(){
return "Car: $this->brand, Color: $this->color";
}
}
$car1 = new Car("Toyota", "Red");
echo $car1->display(); // Output: Car: Toyota, Color: Red
?>
7. NULL:
- NULL is a special type that represents a variable with no value.
- Used to reset a variable or mark “empty”.
NULL
is different from an empty string (""
) or zero (0
).
Example:
PHP
<?php
$x = "Hello";
$x = null;
var_dump($x); // NULL
?>
8. Resource (Special Type):
- A resource is a special variable that holds a reference to an external resource (like database connection, file handle).
- Example: MySQL connection, file opened with
fopen()
.
Example:
PHP
<?php
$file = fopen("test.txt", "r");
var_dump($file); // resource(type=stream)
fclose($file);
?>