Php Datatypes

Hello Readers, Welcome back to Phpkeeda. Today the topic i am going to explain is about DATA TYPES. So the first question arise is :

What is data type : Understand Datatypes in a simple way,  just break Datatypes into two part i.e Data+ types, means types of data.

Datatypes = Data + Type

It represents that which type of data you are going to use. Data may be in the form of string, integer, float, array, object e.t.c

Like other languages PHP also supports different types of datatype. Datatype can be stored in variable and can do different things.

Our PHP supports different types of datatypes which are mentioned below :

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

String : As you might have studied in your previous class that string is a sequence of characters. In php it is also same. Ex: – “Hello php”

A string can be represented inside a single quote or double quote. Ex :  ($variable = “hello php” OR $Variable = “hello php”). Any text which are enclosed inside a single or double quote is a string in php.

Example 

Output :
string(10) “Welcome to”     // $x variable is string type value, having length 10.
string(14) ” phpkeeda.com ”    // $y variable is string type value, having length 14.
string(20) “You are awsome”     // $z  having extra spaces in above program so length is 20.

 

var_dump()  function returns the datatype and the values.

Integer : In PHP, an integer is a non-decimal number having range  -2,147,483,648 and 2,147,483,647. A value is said to be an integer in php if it satisfies these factors :

  • To be an integer a value must contains at least one digit.
  • An integer must not contain decimal values.
  • An integer can be both positive as well as negative.
  • There are three formats to specify an integer  :
    • Decimal – (10 based)
    • hexadecimal – (16-based – prefixed with 0x)
    • octal – (8-based – prefixed with 0).

 Output :  int(5985)

Float : Those number which having decimal point in it or which is in exponential form is called float point number.

Example :

 Output :  float(12.34)

In the above example float value is 1234 inside variable $p and var_dump()  is returning the type of value (i.e float) and the value 12.34.

Boolean : A boolean is a datatype which represents two states  i.e  either true or false.

 

Boolean are mostly used in conditional statement to check that value is either true or false. If you have learnt conditional statement in C programming then you can understand this concept in the below example. Else leave it for now I will discuss it later in conditional statement tutorial.

 

Array : An array is a datatype which stores multiple values in a single variable.

you might not understanding array. We have a complete tutorials over all these datatypes. As of now just understand that array is a datatype which can contain single or multiple values inside it.

Example :

 

 Object :  An object datatype contains functions, contants and variables.

In PHP language, an object must be declared explicitly.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:

To create an object we first create class, if you are familiar with C++ or java you might have heard about class and object, the same concept applies in PHP too.  To create class, keyword class is used,  You will get to know more about class and objects in OOPS tutorial for PHP.

So let’s create a class to understand object.

 

Null : A special datatype which contains only one value : NULL.
Null datatype can be defined as : – A variable which have no value assigned to it.

  • if we create a variable and do not assign any value to it. Then it will automatically assigned NULL or we can assigned null value to a variable. Have look into the below code.

 Output :  NULL

Leave a Reply

Your email address will not be published. Required fields are marked *