Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP Data Types
Next article icon

PHP Data Types

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you don’t need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable, bug-free, and efficient code.

PHP supports 8 main data types, grouped into three categories:

  • Scaler Data Types
  • Compound Data Types
  • Special Data Types
Image
PHP Data Types

Scaler Data Types

Scalar types hold single values. These are the basic and commonly used data types in PHP. They store only one value at a time, such as a number or a string of text.

1. Integer

An integer is a whole number (positive or negative) without decimals.

Now, let us understand with the help of the example: 

PHP
<?php $age = 25; echo $age;  // Output: 25 ?> 

Output
25

2. Float (Double)

A float is a number with a decimal point or in exponential form.

Now, let us understand with the help of the example: 

PHP
<?php $price = 99.99; echo $price;  // Output: 99.99 ?> 

Output
99.99

3. String

A string is a sequence of characters, enclosed in single or double quotes.

Now, let us understand with the help of the example: 

PHP
<?php $name = "GeeksforGeeks"; echo $name;  // Output: GeeksforGeeks ?> 

Output
GeeksforGeeks

Note: Double-quoted strings allow variable parsing; single-quoted strings don’t.

PHP
<?php     $greeting = "Hello"; echo "Message: $greeting  \n";  // Message: Hello echo 'Message: $greeting';  // Message: $greeting ?> 

Output
Message: Hello   Message: $greeting

To learn more about PHP Strings read this article - PHP Strings

4. Boolean

A boolean represents either true or false. It is useful for conditional statements and logic control.

Now, let us understand with the help of the example: 

PHP
<?php $isActive = true; if ($isActive) {     echo "Active"; } ?> 

Output
Active

Compound Data Types

1. Array

Compound types hold multiple values or are used to structure data more meaningfully. These data types allow you to group several values together.

Now, let us understand with the help of the example:

PHP
<?php $colors = ["red", "green", "blue"]; echo $colors[1];  // Output: green ?> 

Output
green

To learn more about PHP Array read this article - PHP Arrays

2. Objects

An object is an instance of a class and is used to access methods and properties. Objects are explicitly declared and created from the new keyword.

Now, let us understand with the help of the example:

PHP
<?php class Car {     public $brand = "Toyota"; } $car = new Car(); echo $car->brand;  // Output: Toyota ?> 

Output
Toyota

To learn more about PHP Obejcts read this article - PHP Objects

Special Data Types

Special types handle unique cases such as representing empty values or connecting with external resources.

1. NULL

A NULL value means the variable has no value. It is unset using unset().

Now, let us understand with the help of the example:

PHP
<?php $x = NULL; var_dump($x);  // Output: NULL ?> 

Output
NULL 

2. Resources

A resource is a special variable that holds a reference to an external resource, such as a file or database connection.

PHP
<?php     $handle = fopen("file.txt", "r"); var_dump($handle);  ?> 

Output
Warning: fopen(file.txt): failed to open stream: No such file or directory in /home/guest/sandbox/Solution.php on line 2 bool(false) 

Note: To check the type and value of an expression, use the var_dump() function which dumps information about a variable. 

PHP Functions to Check Data Types

PHP provides several functions to check the type of variables, such as:

  • is_int(): Checks if a variable is an integer.
  • is_string(): Checks if a variable is a string.
  • is_array(): Checks if a variable is an array.
  • is_object(): Checks if a variable is an object.
  • is_bool(): Checks if a variable is a boolean.
  • is_null(): Checks if a variable is NULL.

Best Practices for using the Data Types

  • Know the type of data your variable holds.
  • Use var_dump() or gettype() for debugging.
  • Avoid mixing types unless necessary.
  • Validate and sanitize data when working with user inputs.

Conclusion

PHP data types are the foundation of how information is stored and handled in your scripts. From simple values like numbers and strings to complex structures like arrays and objects, understanding these types helps you write better and more efficient code. Since PHP automatically assigns data types, it's important to know how they work, how to check them, and how to manage conversions when needed. Mastering data types ensures your programs run smoothly and behave as expected.


Next Article
PHP Data Types

C

Chinmoy Lenka
Improve
Article Tags :
  • Misc
  • Web Technologies
  • PHP
  • PHP-basics
Practice Tags :
  • Misc

Similar Reads

    PHP 8 Union types
    A “union type” accepts values of multiple different data types, rather than a single one. If the programming language supports union types, you can declare a variable in multiple types. For example, there can be a function that can accept the variable of type “string” or “float” as a parameter. PHP
    2 min read
    PHP | Types of Errors
    Errors are an essential part of the development process. They help developers identify issues with their code and ensure that applications run smoothly. PHP provides several types of errors. Understanding these error types is important for troubleshooting and debugging effectively.In this article wi
    3 min read
    PHP | settype() Function
    The settype() function is a built-in function in PHP. The settype() function is used to the set the type of a variable. It is used to set type or modify type of an existing variable. Syntax: boolean settype($variable_name, $type) Parameters: The settype() function accepts two parameters as shown in
    2 min read
    PHP Type Juggling
    Type juggling in PHP refers to PHP's automatic conversion of values between data types during operations, such as arithmetic or comparison. This is also known as implicit casting and happens automatically during runtime.PHP automatically changes data types during operations like addition or comparis
    3 min read
    PHP | Format Specifiers
    Strings are one of the most used Datatype arguably irrespective of the programming language. Strings can be either hardcoded (specified directly by the developer) or formatted (where the basic skeleton is specified and the final string is obtained by incorporating values of other variables). Formatt
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences