PHP Tutorials: Data type

With this topic, the first question comes your mind would be, what is a data type? A data type is a type of data you store in a variable. The term data type is similar for all Programming languages.

Although, PHP variables can accept the value of any data type and you don’t even have to declare it anywhere. You can store a number in a variable in the first line and then string in another line in the same variable. PHP will never warn you about this. Still, we need to know, how many data type PHP supports.

PHP supports 8 types of primitive data types and they are:

  1. Integer
  2. Floating number
  3. String
  4. Boolean
  5. Array
  6. Object
  7. Resource
  8. NULL

Let us see in details about all 8

Integer

An integer is a whole number either positive or negative. An integer is always without decimal points value. Integer can be specified in Decimal (base 10), hexadecimal (base 16 – prefixed with 0x) or octal (base 8 – prefixed with 0). Here is an example of Integer.

<?php 
     $positive_int = 1987;
     $negative_int = -2019;
     $hexa_int = 0x1A;
     $octa_int = 0123;
?>

String

An alphanumeric collection of characters called a string. A string can have numbers, characters, and special characters. A string can be defined using single quotes or double-quotes. Let us take an example

<?php 
     $single_quote_string = 'Hello World! I am Naresh Kumar.';
     $double_quote_string = "Thank you for reading!";
     $escaped_string = 'It\'s my website.';
?>

Floating number

A fractional number is called a floating number. More easily saying a number with decimal points i.e. 10.20. It also called a real number or doubles. Similar to an integer it can be positive or negative. Let us take an example.

<?php 
     $float_number = 10.20;
     $double_number = 40.2e3;
     $real_number = 4E-10;
?>

Booleans

A boolean is a data type which can only contain one of the two values. Either true (1) or false (0). Booleans are used in the comparison, decision making, etc. Let us see an example.

<?php 
     $true_bool = true;
     $false_bool = false;
?>

Arrays

Generally, you might have read somewhere for other programming languages, an array is a collection of the homogeneous data type. Which mean a collection of data of a similar kind stored in a single variable. But in PHP you can store both homogeneous or heterogeneous type of data in a variable.

Why we use an array? When you wanted to store a similar type of data say 100 car names, there is no sense to take 100 variables to store the name of cars. You can take a single array and store all of them in as an array.

Value of array can be accessed with its index which starts from 0 and goes up to the length of array minus 1. Let us see an example.

<?php 
     $car = array( "i20", "Creta", "Verna", "i10" );
     echo $car[1]; //OUTPUT Creta
?>

Objects

Doesn’t it sound very familiar? An object is used to initialize class properties and also allocate memory. An object is a variable type which is used to store values and methods to process any information. Objects are created with a keyword “new”

An object contains methods and properties. Methods are operation a class does and properties are used to store value for a class. Let’s take a live example. A Car is a Class, steering, wheels, and engine are properties. Turing, moving and ignition are methods. An Audi is an object. Let’s code!

<?php

//Class Definition
     class MyClass{
          //Property
          public $my_age = 28;
          //Method
          public function getMyAge(){
               return $this->my_age;
          }
     }
     //Declare an object
     $my_class_object = new MyClass();
     //Class to class method
     echo "My age is ".$my_class_object->getMyAge();
?>

NULL

NULL means empty value. NULL can be used to initialize a variable without any value. But a variable when declared without any value it is default set to NULL in PHP. Let us take an example.

<?php
     $my_null_var = NULL;
     print($my_null_var); //Output will be blank.
?>

Resources

In PHP resources are used to hold a reference to an external service or resource. Resources can be used to connect to Database or handle files in PHP. Let us code!

<?php
     //this variable will hold ref. of connection made.
     $mysql_object = mysql_connect( 'localhost', 'username', 'password' );
?>

I hope this tutorial given you a little clarification about variables in PHP.

4 thoughts on “PHP Tutorials: Data type

Leave a Reply

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