PHP Data Types

In PHP a data type refers to the type of data a variable can store. PHP has eight (8) different data types you can work with.

Scalar types

  • boolean
  • integer
  • float
  • string

Compound types

  • array
  • object

Special types

  • resources
  • NULL

Boolean

In PHP the boolean data type is a primitive data type they have only two possible values true and false. This is a fundamental data type and very common in computer programs.

Syntax

<?php
$isVisited = False;
$isVisited = userVisited();
if ($isVisited) {
    echo "User visited";
} else {
    echo "User did not visited";
}
?>

Integer

Integer is a whole number. PHP integer is the same as the long data type in C. It is a number between -2,147,483,648 and +2,147,483,647. Integers can be specified in three different notations in PHP. Decimal, hexadecimal and octal. Octal values are preceded by 0, hexadecimal by 0x.

Rules for integers

  • Integer must have at least one digit (0-9)
  • Integer must not have a decimal point
  • Integer can be either positive or negative
  • Integer cannot contain comma or blanks

Syntax

<?php
$w = 2300;
var_dump($w);
$x = -560; // This is negative number 
var_dump($x);
$y = 0x2C; // This hexadecimal number
var_dump($y);
$z = 047; // This octal number
var_dump($z);
?>

This will produce following result

int(2300) 
int(-560) 
int(44) 
int(22)

Floating Point Numbers

Float may contain any decimal number or a number in exponential form.Floating point numbers in PHP can be larger than integers. PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point and second is a floating-point number written in scientific notation.

Syntax

<?php
$var1 = 2.786;
$var2 = 2.2e3;
$var3 = 1E-10;
$var4 = 12345678923445;
var_dump($var1);
var_dump($var2);
var_dump($var3);
var_dump($var4);
?>

This will produce following result

float(2.786)
float(2200)
float(1.0E-10)
float(12345678923445)

Strings

String is a series of single characters such as "Hello PHP". Most of working with Web pages is about working with strings. In PHP a string must be delimited by one of four syntaxes: single quotes, double quotes, heredocs, and nowdocs.

Syntax

<?php
$var1 = "My First PHP Programm!";
echo $var1;
$var1 = 'My Second PHP Programm!';
echo $var1;
?>

This will produce following result

My First PHP Programm!
My Second PHP Programm!

Array

In simple word array is complex data type which hold collection of related data elements.Each of the elements can be accessed by an index.

Syntax

<?php
$months = array("June", "July", "August", "September", "October");
print_r($months);
?>

This will produce following result

Array
(
    [0] => June
    [1] => July
    [2] => August
    [3] => September
    [4] => October
)

Object

In simple word object are instances of class definitions. Objects are user defined data types normaly used to define properties/attributes of object. First we need to declear a class of object that can contain properties and methods.

Syntax

<?php
class  Books{
    /* Member variables */
    var $price;
    var $title;
    /* Member functions */
    function setPrice($price_par){
       $this->price = $price_par;
    }
    function getPrice(){
       echo "Book Price: $" .$this->price ."<br/>";
    }
    function setTitle($title_par){
       $this->title = $title_par;
    }
    function getTitle(){
       echo "Title: ".$this->title . "<br/>";
    }
}
// creating instance of class Book
$book = new Books();

// setting properties values
$book->setTitle("PHP Tutorials Book");
$book->setPrice(123);

// show properties
$book->getTitle();
$book->getPrice(); 
?>

This will produce following result

Title: PHP Tutorials Book 
Book Price: $123

Resource

Resources are special data types. They hold a reference to an external resource (such as database connections).

NULL

NULL is a special type that only have one possible value and that's null. This type is also treated as a language constant in PHP and is case-insensitive. PHP considers all undefined variables to be of type NULL.

Syntax

<?php
$var="Hello world!";
$var=null;
var_dump($var);
?>

This will produce following result

NULL


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.