EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Data Types

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Data Types

PHP Data Types

Introduction to PHP Data Types

PHP or Hypertext PreProcessor is a web based application development programming language, that can incorporate HTML coding in them for constructing a web application. In PHP, there are eight different data types that are used for declaring and calling the variables in the script. They are ‘Boolean’ for true or false values, ‘Integer’ for numeric values, ‘Float/Double’ for decimal numerals, ‘String’ for characters, ‘Arrays’ for fixing element size, ‘object’ for representing instances of the class, ‘NULL’ for void, and ‘resources’ for referring to elements outside the PHP script.

Top 3 PHP Data Types

PHP variables used to store values may be associated with all kinds of data types ranging from the simplest int to more complicated data types such as arrays. PHP is called a loosely typed Programming language, which means the variable data types are decided based on its attributes during run-time and is not explicitly defined. It analyses the attributes of the value given and then determines the data type to be assigned to it. There are 8 primitive data types which PHP supports and which can be further classified to 3 types as below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Let us go through each one of them in detail with an example each.

Top PHP Data Types

1. Scalar Types

They can be further divided into primitive types as below:

a. Boolean

These types have their possible output in the form of either 0 or 1 i.e. true or false. They are used for conditional testing cases where the event returns true when the condition is satisfied and false when it does not satisfy. It also considers NULL and empty string as false.

Code:

<?php
// TRUE is assigned to a variable value
$variable_value = true;
var_dump($variable_value);
?>

Output:

PHP Data Types eg1

b. Integer

An integer data type holds non-decimal whole number values between -2,147,483,648 and 2,147,483,647. This maximum and minimum value depends on the system whether it is 32-bit or 64-bit. By using the constant PHP_INT_MAX we can find out the max value. Also holds base 10, base 8 and base 6 values.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
// example for decimal (base 10)
$dec1 = 100;
$dec2 = 200;
// example for decimal (base 8)
$oct1 = 10;
// example for decimal (base 6)
$hex1 = 0x15;
$addn = $dec1 + $dec2;
echo $addn;
?>

Output:

PHP Data Types eg2

c. Float/ Double

A number having decimal point or an exponent is called a floating-point number/ real number. It can have both positive and negative numbers. There shall be a pre-defined number of decimal places displayed for the number.

Code:

<?php
$dec1 = 0.134;
var_dump($dec1);
$exp1 = 23.3e2;
var_dump($exp1);
$exp2 = 6E-9;
var_dump($exp2);
?>

Output:

PHP Data Types eg3

d. String

A string data type is basically a collection of characters including numbers, alphabets, and letters. They can hold values up to 2GB. They are to be declared using double quotes if a variable has to be displayed amongst the string. Else, a single quote also works.

Code:

<?php
$name = "Jay";
$str1 = 'Declaring name in single quote as $name';
echo $str1;
echo "\n";
$str2 = "Declaring name in double quote as $name";
echo $str2;
echo "\n";
$str3 = 'Just a string';
echo $str3;
?>

Output:

PHP Data Types eg4

2. Compound Types

These are the ones for which new values cannot be assigned. Arrays and objects fall under this category.

a. Arrays

It is a data structure having a collection of fixed size of elements with similar data types. It is also used to store the known amount of key-value pairs in the form of an ordered map in it. It can be used for various purposes like a list, hash table (map implementation), collection, stack, dictionary, queue, etc, Multi-dimensional arrays are also possible.

A simple example of an array is as follows:

Code:

<?php
$animals = array("Dog", "Cat", "Cow");
var_dump($animals);
$animal_babies = array(
"Dog" => "Puppy",
"Cat" => "Kitten",
"Cow" => "Calf"
);
var_dump($animal_babies);
?>

Output:

eg5

b. Objects

It allows to store data (called its properties) and also gives information on how to process (called the methods of the object) the same. An object serves as an instance of a class which is used as templates for other objects. The keyword “new” is used for the creation of an object.

Each object inherits the properties and methods from that of the parent class. It requires an explicit declaration and a “class” in each object.

Code:

<?php
// Declaring a class
class statement{
// properties
public $stmt = "Insert any string here";
// Declaring a method
function show_statement(){
return $this->stmt;
}
}
// Creation of new object
$msg = new statement;
var_dump($msg);
?>

Output:

eg6

3. Special Types

There are 2 special data types in PHP which fall under this category since they are unique. They are:

a. NULL

In PHP, this special NULL is used for representing empty variables i.e. the variable has no data in it and NULL is the only possible value to it. A variable assigned to the constant NULL, if it has been set to unset() or if no value has been set to it becomes a NULL data type.

Here we are setting NULL directly to val1. Whereas, for the val2 variable, we are assigning a string value first and then set it as NULL. In both cases the final value of variables is NULL.

Code:

<?php
$val1 = NULL;
var_dump($val1);
echo "<br>";
$val2 = "Any string";
$val2 = NULL;
var_dump($val2);
?>

Output:

 eg7

b. Resources

Resource is not an actual data type whereas it is a special variable that keeps a reference to a resource external to PHP. They hold special handlers for files and database connections that are open. Special functions usually create and use these resources.

To run this code, we must have the file.txt created in the system with read permission given to it. It throws an error in case “handle” is not a resource. Also, make sure to connect to any existing database in your system.

Code:

<?php
// Open an existing file to read
$handle = fopen("file.txt", "r");
var_dump($handle);
echo "<br>";
// Connecting to MySQL database server with settings set to default
$db = mysql_connect("localhost", "root", "");
var_dump($db);
?>

Apart from the above data types, we also have something called pseudo-types which are the keywords in PHP document used to indicate the types or values which an argument can have. Some of them are:

  • mixed: They allow a parameter to accept more than one type. Ex: gettype()
  • number: With number, a parameter can be afloat or an integer.
  • void, callback, array|object are some of the other pseudo-types

Conclusion

Here we have covered almost all of the data types which are available in PHP. All of the above 8 primitive types are implicitly supported by PHP and there is no need for the user to specify them manually. Arrays and objects can hold multiple values whereas for rest all can hold only a single value (except NULL which holds no value).

Recommended Articles

This is a guide to PHP Data Types. Here we discuss the top 3 PHP data types such as scalar, compound, and special in detail along with examples and implementation. You may also look at the following articles to learn more-

  1. Functions in PHP
  2. Inheritance in PHP
  3. Introduction To PHP
  4. PHP Form

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More