PHP Data Types


Data Types in PHP

PHP supports total Ten primitive data types in 3 categories: scaler types(boolean, integer, float and string), compound types(array, object, callable and iterable), and special types (resource, NULL).

  • boolean
  • string
  • integer
  • float (double)
  • array
  • object
  • callable
  • iterable
  • resource
  • NULL

PHP boolean data type

Boolean is simple data type, it works like switch. It can be either TRUE(1) or FALSE(0), and both are case-insensitive.

$test = TRUE;
if($test==1)
{
echo 'yes';
} 
else 
{
echo 'No';
}
//Output :: yes

$test = TRUE;
if($test)
{
echo 'yes';
} 
else 
{
echo 'No';
}
//Output :: yes

$test = TRUE;
if($test==True)
{
echo 'yes';
} 
else 
{
echo 'No';
}
//Output :: yes


PHP string data type

A string is set of characters, like "This is string", Every character is same as byte. Simple way of define string in single quotes or double quotes like 'Testing' OR "Testing".

$string1 ='Test String';
$string2 ="Test String";

PHP integer data type

An integer data type is number like (..,-3,-2,-1,-,1,2,3, ..). Integer can be positive or negative without decimal point.

Integer can decimal(base 10), hexadecimal (base 16 prefixd with 0x),octal (base 8 prefixed with 0) or binary.

$b = -456; // a negative number
var_dump($b);
//Output :: -456

$a = 456; // decimal number
var_dump($a);
//Output :: 456 
  
$c = 0x1B; // hexadecimal number
var_dump($c);
//Output :: 27 
 
$d = 0128; // octal number
var_dump($d);
//Output :: 10 

PHP float data type

Floating point numbers is a number with a decimal point, this also knows floats, doubles, or real numbers.

$a = 12.01; 
var_dump($a);
 
$a = 1234.456; 
var_dump($a);

$b = 1.3e3; 
var_dump($b);

$c = 8E-10;
var_dump($c);

PHP array data type

An array is a data structure set of elements that store multiple values in a single variable.

$array = array(
    'key1'  =>'value1',
    'key2'  =>'value2'
);

PHP object data type

An object is a data structure, obeject refers to instance of a class. object creates by "new" keyword

It must be explicitly declared in PHP.

class Student
{
	public $name = 'Anik Kumar';	
}
// craete an object
$obj = new Student();

//access the properties
echo $obj->name;

PHP callable data type

Callable is a data-type, it's represent by callable type hint.

Some of the PHP functions allows user defined callback function as a parameter like call_user_func() or usort().

In callback functions can also pass the object methods, static class methods. language constructs can not pass as a parameter( echo,print,isset(), etc)

// An example of callback function
function test_callback_function() {
    echo 'Hi!';
}

// An example of callback method
class ClassA {
    static function testCallbackMethod() {
        echo 'Hi';
    }
}

// Example 1: Simple callback, test_callback_function passed as a parameter
call_user_func('test_callback_function');

// Example 2: Static class method call, passed array as a parameter , first value is class name and another is method name
call_user_func(array('ClassA', 'testCallbackMethod'));

// Example 3: Object method call, passed array as a parameter , first value is object of class and another is method name
$obj = new ClassA();
call_user_func(array($obj, 'testCallbackMethod'));

// Example 4: Static class method call, passed class name and method name as static
call_user_func('ClassA::testCallbackMethod');

PHP NULL data type

The special NULL value represents an empty variable means no value.

    A variable is NULL if
  • It has been unset().
  • Assigned constant NULL
  • A vaiable is not set any value/li>
// An example of callback function
function test_callback_function() {
    echo 'Hi!';
}