codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

4 / 56 topics
1Getting Started with PHP2PHP Syntax3Variables in PHP4Data Types in PHP5Constants in PHP6Operators in PHP
Tutorials/PHP/Data Types in PHP
🐘PHP

Data Types in PHP

Updated 2026-05-15
10 min read

Data Types in PHP

Introduction

PHP is a versatile and widely-used server-side scripting language. Understanding the various data types available in PHP is fundamental to writing effective and efficient code. In this tutorial, we will explore the different data types that PHP supports, including their characteristics and practical examples.

Concept

Data types are essential for storing and manipulating data within a program. PHP supports several built-in data types, which can be broadly categorized into scalar types, compound types, and special types. Let's delve into each of these categories to understand how they work in PHP.

Scalar Types

Scalar types represent single values and include:

  1. Integer: Represents whole numbers without a decimal point.
  2. Float (Double): Represents floating-point numbers with a decimal point.
  3. String: Represents text data.
  4. Boolean: Represents true or false values.

Compound Types

Compound types are used to group multiple values together and include:

  1. Array: An ordered map that can hold multiple values.
  2. Object: Instances of classes, which can have properties and methods.

Special Types

Special types include:

  1. Resource: Represents external resources like database connections or file handles.
  2. NULL: Represents a variable with no value.

Examples

Let's explore each data type with practical examples to solidify our understanding.

Integer

Integers are whole numbers without a decimal point. They can be positive, negative, or zero.

php
1<?php
2$age = 25;
3echo $age; // Outputs: 25
4?>

Float (Double)

Floats represent floating-point numbers with a decimal point.

php
1<?php
2$price = 19.99;
3echo $price; // Outputs: 19.99
4?>

String

Strings are used to store text data. They can be enclosed in single quotes, double quotes, or heredoc syntax.

php
1<?php
2$name = 'John Doe';
3echo $name; // Outputs: John Doe
4
5$greeting = "Hello, $name!";
6echo $greeting; // Outputs: Hello, John Doe!
7
8$description = <<<EOT
9This is a heredoc string.
10It can span multiple lines.
11EOT;
12echo $description;
13?>

Boolean

Booleans represent true or false values. They are often used in conditional statements.

php
1<?php
2$isStudent = true;
3if ($isStudent) {
4 echo 'This person is a student.';
5} else {
6 echo 'This person is not a student.';
7}
8?>

Array

Arrays are ordered maps that can hold multiple values. They can be indexed or associative.

php
1<?php
2$fruits = ['apple', 'banana', 'cherry'];
3echo $fruits[0]; // Outputs: apple
4
5$person = [
6 'name' => 'Alice',
7 'age' => 30,
8 'isStudent' => false
9];
10echo $person['name']; // Outputs: Alice
11?>

Object

Objects are instances of classes, which can have properties and methods.

php
1<?php
2class Car {
3 public $color;
4
5 function __construct($color) {
6 $this->color = $color;
7 }
8
9 function displayColor() {
10 echo "The car color is: " . $this->color;
11 }
12}
13
14$myCar = new Car('red');
15$myCar->displayColor(); // Outputs: The car color is: red
16?>

Resource

Resources represent external resources like database connections or file handles.

php
1<?php
2$file = fopen("example.txt", "r");
3if ($file) {
4 echo 'File opened successfully.';
5} else {
6 echo 'Failed to open file.';
7}
8fclose($file);
9?>

NULL

NULL represents a variable with no value.

php
1<?php
2$variable = null;
3var_dump($variable); // Outputs: NULL
4?>

What's Next?

In the next section, we will explore constants in PHP, which are similar to variables but have fixed values that cannot be changed during the execution of a script.

Stay tuned for more tutorials on PHP and other programming topics!


PreviousVariables in PHPNext Constants in PHP

Recommended Gear

Variables in PHPConstants in PHP