Table of contents
1.
Introduction
2.
PHP Boolean
2.1.
PHP
3.
PHP Integer
3.1.
PHP
4.
PHP Float
4.1.
PHP
5.
PHP String
5.1.
PHP
6.
PHP Array
6.1.
PHP
7.
PHP object
7.1.
PHP
8.
PHP Resource
9.
PHP Null
9.1.
PHP
10.
Frequently Asked Questions
10.1.
What is PHP and its types?
10.2.
What is PHP used for?
10.3.
What is PHP concept?
11.
Conclusion
Last Updated: Nov 16, 2024
Easy

Data Types in PHP

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we learn about PHP data types. PHP uses data types to hold different types of data or values. We can divide PHP data types into the following three types:

  • Scalar Type - Scalar Types are predefined PHP data types that hold only a single value. There are four types of scalar data types in PHP.
    • boolean
    • integer
    • float
    • string
  • Compound Type - Compound Types are user-defined PHP data types that hold multiple values. Compound data types are of two types.
    • array
    • object
  • Special Type - There are two special types of data types in PHP.
    • resource
    • NULL
PHP Data Types

PHP Boolean

Booleans are the most specific PHP data types that work as a toggle ON/OFF switch. It can hold only two values, 1 for TRUE and 0 for FALSE. We often use Booleans in conditional testing.

  • PHP

PHP

<?php
   $var = true;
   if ($var)
       echo "This condition is TRUE.";
   else
       echo "This condition is FALSE.";
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP Integer

An Integer is positive or negative numeric data without a fractional part. An Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16) with a range between 2,147,483,648 and 2,147,483,647, that is, -2^31 to 2^31.

  • PHP

PHP

<?php
   $bin = 0b11;
   $dec = 96;
   $oct = 0243;
   $hex = 0x45;
   echo "Binary number: " .$bin. "\n";
   echo "Decimal number: " .$dec. "\n";
   echo "Octal number: " .$oct. "\n";
   echo "HexaDecimal number: " .$hex. "\n";
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP Float

A float is a number with a decimal point. It can hold positive or negative numbers with a fractional or decimal point. 

  • PHP

PHP

<?php   
   $n1 = 22.69; 
   $n2 = 88.96; 
   $sum = $n1 + $n2; 
   echo "Sum of the two float numbers: " .$sum; 
?> 
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP String

A string is an array of characters. It holds numbers, alphabets, and special symbols. We enclose the string between single or double quotes, and both are different in PHP.

  • PHP

PHP

<?php   
   $var = "CodingNinjas"; 
   //both single & double quote statements are different 
   echo "$var"; 
   echo "\n"; 
   echo '$var'; 
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP Array

An array is a collection of homogeneous PHP data types stored under the same name.

  • PHP

PHP

<?php   
   $cars = array ("Rolls Royce", "Bugatti", "Lamborghini"); 
   var_dump($cars); //the var_dump() function returns the datatype and values 
   echo "\n"; 
   echo "Array Element1: $cars[0] \n"; 
   echo "Array Element2: $cars[1] \n"; 
   echo "Array Element3: $cars[2] \n"; 
?> 
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP object

Objects are the instance of the class. Objects are user-defined PHP data types and can store both values and functions.  

  • PHP

PHP

<?php   
    class car { 
         function model() { 
              $model_name = "TATA"; 
              echo "Car Model: " .$model_name; 
            } 
    } 
    $obj = new car(); 
    $obj -> model(); 
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

output

PHP Resource

The PHP resource type is not an actual data type. These are storing a reference to functions and resources external to PHP.

For example, a database call is an external resource.

PHP Null

Null is a particular data type that only has the value NULL. It defines a variable with no value.

Note: The convention of writing null is like "NULL," all in capital letters as it is case-sensitive.  

  • PHP

PHP

<?php   
   $var = NULL; 
   echo $var;
?> 
You can also try this code with Online PHP Compiler
Run Code

Output:

output

Note: NULL gives no output.

Must Read PHP Projects With Source Code

Frequently Asked Questions

What is PHP and its types?

PHP (Hypertext Preprocessor) is a server-side scripting language. Its types include procedural, object-oriented, and mixed programming styles.

What is PHP used for?

PHP is primarily used for web development to create dynamic and interactive web pages, server-side scripting, and database interactions.

What is PHP concept?

PHP is based on the concept of embedding code within HTML to process requests, interact with databases, and generate dynamic content on websites.

Conclusion

In this article, we have discussed Data Types in PHP. Understanding data types in PHP is essential for efficient programming and ensuring that variables are used correctly. PHP offers various data types, including integers, strings, arrays, and objects, which can be leveraged to create dynamic and powerful web applications. By mastering these data types and their nuances, developers can write more optimized, error-free code that interacts smoothly with databases and performs well in a web environment.

Recommended Readings:


Click here to see other related blogs on PHP.

To study more about data types, refer to Abstract Data Types in C++ and Data Types in C++.

Also, check out our web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Code360 is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Live masterclass