PHP Data Types

In this tutorial, you will learn about different types of data types available in PHP.


PHP Data Types means types of data PHP can process. These can be numeric or non-numeric.

PHP supports 10 primitive types of data types that can be further categorized into 3 types.

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

1) PHP Data Types: Scalar Types

This type of data type holds only a single value in it. There are 4 types of scalar data types in PHP.

  1. Boolean
  2. Integer
  3. Float
  4. String

2) PHP Data Types: Compound Types

Four types of compound data types are present in PHP. These kinds of data types can hold more than one value in it.

  1. Array
  2. Object
  3. Callable
  4. Iterable

3) PHP Data Types: Special Types

There are 2 types of special data types are available in PHP.

  1. NULL
  2. Resource

PHP Booleans

Booleans are like a switch, it can have only two possible values, either 0 (True) or 1 (False). These data types are often used with conditional statements. It returns TRUE in case the condition is correct, otherwise FALSE.

Example

<?php
if (TRUE)
echo "TRUE condition executed.";
if (FALSE)
echo "FALSE condition excuted.";
?>

Output

TRUE condition executed.

PHP Integer

Integer datatype holds only numbers without fractional or decimal parts. It can be a positive or negative value. The value of the integer must be between -2^31 and +2^31 i.e. 2,147,483,648 and 2,147,483,647.

Integer value can be represented by decimal (base 10), octal (base 8), or hexadecimal (base 16).

Example

<?php
$a = 251; // a positive number
var_dump($a);
echo "<br>";

$b = -251; // a negative number
var_dump($b);
echo "<br>";

$c = 0x5A; // hexadecimal number
var_dump($c);
echo "<br>";

$d = 0143; // octal number
var_dump($d);
?>

Output

int(251)
int(-251)
int(90)
int(99)

PHP String

PHP String data type holds non-numeric values. It can hold any characters, letters, numbers or even special characters.

String values must be enclosed with single quotes or double quotes

Example

<?php
$a = 'PHP String Example!';
echo $a;
echo "<br>";

$b = "PHP String Example!";
echo $b;
echo "<br>";

$c = 'This is the basic of PHP String.';
echo $c;
?>

Output

PHP String Example!
PHP String Example!
This is the basic of PHP String.

PHP Float

PHP Float data types can hold numbers with a fraction or decimal point, including a negative or positive sign.

Example

<?php
$a = 12.255;
$b = 10.250;
$sub = $a - $b;
echo "Subtraction of floating numbers: " .$sub;
?>

Output

Subtraction of floating numbers: 2.005

PHP Array

An array is a compound data type in PHP. It can hold more than one value at a time of the same data type.

It is mainly useful when you want to aggregate series of related items together, for example, a set of country names.

Example

<?php
$color = array ("Green", "Red", "Blue");
var_dump($color); //the var_dump() function returns the datatype and values
echo "</br>";
echo "Array Element1: $color[0] </br>";
echo "Array Element2: $color[1] </br>";
echo "Array Element3: $color[2] </br>";
?>

Output

array(3) { [0]=> string(5) “Green” [1]=> string(3) “Red” [2]=> string(4) “Blue” }
Array Element1: Green
Array Element2: Red
Array Element3: Blue

PHP Object

PHP Object data types are used to hold both values and functions of user-defined classes. This is the instance of a user-defined class. Objects are declared using a new keyword.

Example

<?php
// Class definition
class color{
function show_color(){
$clr = "Green!";
echo "Favourite Color:" .$clr;
}
}

// Create object from class
$obj = new color;
$obj -> show_color();
?>

Output

Favourite Color:Green!

PHP NULL

The NULL value represents an empty value in PHP. NULL type variable in PHP refers to a variable without any data.

The only possible value of data types null is NULL.

Example

<?php
$a = NULL;
var_dump($a);
echo $a; //This will not give any output
?>

Output

NULL

PHP Resource

PHP resource is a special data type that is mainly used to hold the reference of an external resource.

Resource variables generally hold special handlers to an opened database and file connections.

Example

<?php
// Open a file for reading
$handle = fopen("sample.txt", "r");
var_dump($handle);
echo "<br>";

// Connect to MySQL database server with default setting
$link = mysql_connect("localhost", "root", "");
var_dump($link);
?>

Please get connected & share!

Advertisement