Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Data Types in PHP

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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 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
    $var = true;
    if ($var)
        echo "This condition is TRUE.";
    else
        echo "This condition is FALSE.";
?>

 

Output:

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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
    $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";
?>

 

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   
    $n1 = 22.69;  
    $n2 = 88.96;  
    $sum = $n1 + $n2;  
    echo "Sum of the two float numbers: " .$sum;  
?>  

 

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   
    $var = "CodingNinjas";  
    //both single & double quote statements are different  
    echo "$var";  
    echo "\n";  
    echo '$var';  
?> 

 

Output:

PHP Array

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

<?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";  
?>  

 

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   
     class car {  
          function model() {  
               $model_name = "TATA";  
               echo "Car Model: " .$model_name;  
             }  
     }  
     $obj = new car();  
     $obj -> model();  
?>  

 

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   
    $var = NULL;  
    echo $var;
?>  

 

Output:

Note: NULL gives no output.

Must Read PHP Projects With Source Code

Frequently Asked Questions

1. What is PHP?

PHP is a backend scripting language used for web development. It is an open-source, interpreted server-side scripting language and supports object-oriented programming.

2. What are the advantages of using PHP?

There are many advantages of using PHP over other languages. PHP is a server-side scripting language suited for creating dynamic web pages. It is highly flexible and provides compatibility and easy integration. It provides efficiency in performance and is cost-efficient. 

3. What are Data Types?

A PHP data type is an attribute that tells the compiler or interpreter how the programmer should use the data. A data type constrains an expression's values, such as a variable or a function. The data type defines the operations that we can do on the data, the meaning, and the ways to store values of that type. A data type delivers a set of values from which an expression may take its values.

Key Takeaways

This article teaches about PHP Data Types and how we use them. We saw why it could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.

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, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Topics covered
1.
Introduction
2.
PHP Boolean
3.
PHP Integer
4.
PHP Float
5.
PHP String
6.
PHP Array
7.
PHP object
8.
PHP Resource
9.
PHP Null
10.
Frequently Asked Questions
11.
Key Takeaways