PHP Data Types

In this tutorial we will learn about different data types used in PHP.  The variable which is defined in PHP stores some values. Since PHP is loosely typed language you need not to define the data type for variable every time you try to create any variable, but still there may be situation when it is required to identify the data type.

Data type specifies the type of data you can store in your variable.  Lets categorize the data types into two sets.

  1. Primary data types
    1. Integer
    2. Float
    3. Boolean
  2. Secondary Data types
    1. Array
    2. String

Primary Data Types

It is also known as basic data types. The primary data types are integer (int) , float , Boolean.

Integer

When we are using data type as integer it means the value which we will be assigning to the variable should be a collection of decimal digits without decimal point. An integer variable occupies 16 bit (2 Byte) memory , it stores numbers ranging from -32768 to +32767. The value which we want to assign should be of this range.

For Ex.

$a=100;

$num=1234;

Float

When we want to store real numbers with floating points in such case float data type is used. The memory size of a float variable is 32 bit (4 byte), and its range is 3.4e-38 to 3.4e+38.  In float variable 6 maximum 6 digits are allowed after decimal point.

If we want to store values which are higher than the given range in such case the higher version of float is used which is double. The size of double variable is 64 bit (8 byte). The range of values which are supported by double variable is 1.7e-308 to 1.7e+308. In double maximum 12 digits are allowed after the decimal point. In short double is a higher version of float which has more capacity in terms of range of numbers to be stored.

For Ex.

$percentage=65.5;

Boolean

This data type accepts only two values that is either true or false. The memory size occupied by Boolean variable is 8 bit (1 byte).

For Ex.

$flag=true;

$check=false;

Secondary Data Types

Since they are derived from basic data types, Secondary data types are also known as secondary data types. Following are some of the derived data types used in PHP.

Array

An array is a collection of variables of same type. It’s a derived or secondary data type. We will learn about array in detail in coming tutorial. For now we will keep one thing in mind that whenever we want to store more than one value at a time we will use array.

String

This is a derived data type. When there are combination of more than one characters to be used then it come in category of string.

A string is a mixture of each characters present on your keyboard.

gettype()

This type of function provides us the information about the data type the variable has. The return type of this function is mixed as variable which contains digit will return integer  data type, the decimal point will return float or double

Syntax

            Mixed gettype(Mixed variable/Value)

For Ex.

<?php
                $pid=51;
                $pname=”Butter”;
                $price=12.50;
                $available=true;
                echo gettype($pid);
                echo gettype($pname);
                echo gettype($price);
                echo gettype($available);
?>

In above example pid stores integer value so gettype($pid) will return integer same way for gettype($pname) will return string , gettype($price) will return double & gettype($available) will return boolean.

settype()

This function in PHP is used to set the data type of the variable which is required. One can say that it is a type conversion function. It will convert any variable into any type which is required.

Syntax

       void settype(Mixed variable or value , new data type)

For Ex.

<?php
               $value=10;
               echo gettype($value);
               settype($value,double);
               echo gettype($value);
?>

The output will initially return integer as data type of variable $value , later in program we have changed the data type of $value using settype() function to double so for second time it will return double as data type of $value.

To conclude in this tutorial we have learned the basic data types used in PHP , there size and range of values along with two important functions used with data types that is gettype() & settype().

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments