-
Hajipur, Bihar, 844101
In PHP, data types define the kind of data a variable can hold. Since PHP is a loosely typed language, you don’t need to explicitly declare the data type. PHP automatically determines it based on the value assigned. Understanding data types is essential because it affects how data is stored, manipulated, and displayed.
PHP has several built-in data types. The most commonly used are:
String – sequences of characters
Integer – whole numbers
Float (Double) – decimal numbers
Boolean – true or false
Array – a collection of values
Object – instances of classes
NULL – represents no value
Resource – holds external resources like database connections
A string is a sequence of characters enclosed in single ('
) or double ("
) quotes.
<?php
// Using double quotes
$name = "Vicky";
echo "My name is $name<br>"; // Variable interpolation works
// Using single quotes
$city = 'Delhi';
echo 'I live in ' . $city . '<br>'; // Concatenation required
?>
Double quotes allow variable interpolation
Single quotes display the text literally
Strings can contain letters, numbers, and symbols
Integers are whole numbers without decimal points.
<?php
$age = 25; // Positive integer
$year = -2025; // Negative integer
echo "Age: $age<br>Year: $year<br>";
?>
Can be positive, negative, or zero
Used for counting, indexing, or calculations
Floats (also called doubles) are numbers with decimal points.
<?php
$price = 49.99;
$temperature = -12.5;
echo "Price: $" . $price . "<br>";
echo "Temperature: " . $temperature . "°C<br>";
?>
Used for precise measurements, prices, or scientific calculations
Can also be written in exponential form: $num = 1.5e3;
(1500)
Booleans have only two possible values: true
or false
.
<?php
$isAvailable = true;
$isFinished = false;
// Display boolean values
echo "Availability: " . $isAvailable . "<br>"; // Outputs 1 for true
echo "Finished: " . $isFinished . "<br>"; // Outputs nothing for false
?>
Often used in conditional statements
Helps control program flow
An array is a collection of values stored under a single variable.
<?php
$fruits = array("Apple", "Banana", "Mango");
// Accessing array elements
echo "First fruit: " . $fruits[0] . "<br>";
echo "Second fruit: " . $fruits[1] . "<br>";
// Associative array
$ages = array("Vicky" => 25, "Sanjana" => 22);
echo "Vicky's age: " . $ages["Vicky"] . "<br>";
?>
Arrays can be indexed (numeric keys) or associative (custom keys)
Useful for storing multiple related values
Objects are instances of classes and allow storing data and functions together.
<?php
class Person {
public $name;
public $age;
function __construct($name, $age){
$this->name = $name;
$this->age = $age;
}
function greet() {
echo "Hello, I am " . $this->name . " and I am " . $this->age . " years old.<br>";
}
}
$person1 = new Person("Vicky", 25);
$person1->greet();
?>
Objects are used in object-oriented programming
Encapsulate data (properties) and behavior (methods)
NULL represents a variable with no value.
<?php
$emptyVar = NULL;
echo "Value: " . $emptyVar . "<br>"; // Outputs nothing
?>
Can be assigned explicitly or occur when a variable is declared but not assigned
Useful for resetting variables
Resources hold references to external resources like database connections or files.
<?php
// Example: opening a file creates a resource
$file = fopen("example.txt", "r");
var_dump($file); // Shows resource type
fclose($file);
?>
Resources are temporary and system-specific
Cannot be directly manipulated like strings or numbers
You can check a variable’s data type using the gettype()
function.
<?php
$name = "Vicky";
$age = 25;
$isAvailable = true;
echo gettype($name) . "<br>"; // string
echo gettype($age) . "<br>"; // integer
echo gettype($isAvailable) . "<br>"; // boolean
?>
Useful for debugging or validating input
Can also use var_dump()
to see both value and type
PHP is loosely typed; data types are assigned automatically
Common data types: string, integer, float, boolean, array, object, NULL, resource
Strings can use single or double quotes
Arrays can be indexed or associative
Objects encapsulate properties and methods
Boolean values are used in conditions, and NULL represents no value
Resources reference external system objects like files or database connections
Mastering data types is essential because every variable, operation, and function relies on the correct type. Understanding them ensures your PHP code runs correctly and efficiently.
Create a PHP script that declares a string variable $name = "Vicky"
and prints it using echo
. Add a comment explaining that it is a string.
Declare an integer variable $age = 25
and a float variable $height = 5.7
. Print both using echo
with comments describing each type.
Create a boolean variable $isAvailable = true
and use it in an if
statement to print “Item available” if true, and “Item not available” if false. Include comments explaining the condition.
Define an indexed array $fruits = array("Apple", "Banana", "Mango")
and print the first and third element using echo
. Add comments describing the array elements.
Create an associative array $ages = array("Vicky" => 25, "Sanjana" => 22)
and print each person’s age with comments explaining the key-value relationship.
Write a PHP class Person
with properties $name
and $age
, and a method greet()
that prints a greeting. Instantiate an object and call the method. Include comments explaining each part.
Declare a variable $emptyVar = NULL
and print it. Add a comment explaining the purpose of the NULL type.
Open a file named example.txt
using fopen()
and store it in a variable $file
. Use var_dump()
to show the resource type and include comments explaining what a resource is.
Use the gettype()
function to check the data type of $name
, $age
, and $isAvailable
. Print the results and include comments explaining each type.
Create a PHP script that declares a variable $price = 49.99
, prints it using echo
, and then prints its type using gettype()
. Include comments explaining that it is a float.