Core PHP PHP

PHP 5 Data Types

PHP Data Types

A variable can store different types of data, and they can do different things.

Following data types are support in PHP:

  • String
  • Integer
  • Float (floating point numbers and also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String Data Types

A string is a collection of characters, like “Hello Friends!”.

You can use single or double quotes for string:

Example

<?php
$a = “Hello Friends!”;
$b = ‘Hello Friends!’;

echo $a;
echo “<br>”;
echo $b;
?>

 

Output

Hello Friends!

Hello Friends!

 

PHP Integer Data Types

An integer data type is a number between -2,147,483,648 and 2,147,483,647. This is non decimal value. An integer can be either positive or negative value. Integer can specified in decimal value (base 10), hexadecimal (base 16 – prefixed with 0x) or octal (base 8 – prefixed with 0)

Example

<?php

// Decimal number

$a = 90;

var_dump($a);

echo “<br>”;

 

// Negative number

$a = -90;

var_dump($a);

echo “<br>”;

 

// Hexadecimal number

$a = 5A;

var_dump($a);

echo “<br>”;

 

// Octal number

$a = 090;

var_dump($a);

?>

PHP Float Data Types

A float (floating point) is a number with decimal point value.

In the following example $x is a float. The PHP var_dump() function returns the data type and value:

Example

<?php
$a = 24.23;

var_dump($a);
?>

PHP Boolean Data Types

A Boolean represents TRUE or FALSE.

Example

<?php

$a = TRUE;
$b = FALSE;

var_dump($a);

var_dump($b);

?>

Boolean is used in conditional testing.

 

PHP Array Data Types

An array is a type of data structure that can contain a fixed size sequential collection of element of same data type. Or you can say that an array is a collectin of multiple values in one single variable.

Example

<?php
$names = array(“Shyam”,”Sudhir”,”Kunal”,”Tushar”);

var_dump($names);

?>

 

PHP Object Data Types

In PHP an object is a data type that not only allows to store data, also information on, how to process the data. An object is a instance of a class which serve as template for objects.

Objects are created based on class with the help of new keyword.

Every object has properties and methods corresponding to class.

We must declare a class of object. For create a class use the keyword class. A class is a structure that contains objects:

Example

<?php
class A {

Public $var = “Hello Friends !”;
function Show() {
return $this->var;
}
}

// create an object
$obj = new A();

// show object properties
var_dump($obj);
?>

PHP NULL Value Data Types

In PHP NULL is a special data type which means there is no value or empty value assigned to a variable. If you create a variable without value, it automatically assign NULL value.

Example

<?php
$a = “Hello Friends!”;
$a = NULL;
var_dump($a);
?>

 

PHP Resource Data Types

In PHP resource is a special type of variable, holding a reference to an external resource.

Resource variables used by special functions.

See also the get_resource_type() function.

 

Example

<?php

// mysql link

$con = mysql_connect();

echo get_resource_type($con);

echo “<br>”;

 

// stream

$fopen = fopen(“test.txt”, “w”);

echo get_resource_type($fopen);

echo “<br>”;

 

// domxml document

$doc = new_xmldoc(“1.0”);

echo get_resource_type($doc->doc);

?>

Leave a Reply

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