Home >>PHP Tutorial >PHP Data Types

PHP Data Types

PHP data types

Data types specify the size and type of values that can be stored.

Variable does not need to be declared ITS DATA TYPE adding a value to it.

PHP is a Loosely Typed Language so here no need to define data type.

To check only data type use gettype( ) function.

To check value, data type and size use var_dump( ) function.

for eg : variable contains integer, float, and string value




Output int(100) float(100) string(5) "Hello"

In the above example declare three variable $num , $fnum , $str.

$num hold value="100". (contain integer value).

$fnum hold value=100.0 (contain float value).

$str hold value="Hello" (contain string value).

Data types in PHP

There are 3 types of DATA TYPE

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

Scalar(It holds only single value)

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

Integer Data type

Integer means numeric data types. A whole number with no fractional component.

Integer may be less than greater than or equal to zero.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed).

64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit Integer value should be between -2,147,483,648 and 2,147,483,647




Output int(100)

In the above example $num hold value=100. pass this variable with echo statement to print the value:

Float/double Data type

It is also called numeric data types.A number with a fractional component.





Output float(100)

$num hold value=100.0. pass $num inside echo statement to display the output.

String Data type

Non numeric data type String can hold letters,numbers and special characters.

String value must be enclosed eighter in single quotes or double quotes.

$str="Welcome user";
$str1='how r you?';




Output string(12) "Welcome user" string(10) "how r you?" string(1) "@"

In the above example We create three variable to hold three string values. To display the output pass all three variables with echo output will display.

Boolean Data type

Boolean are the simplest data type.Like a switch that has only two states ON means true(1) and OFF means false(0).



Output bool(true) bool(false)

In the above example declare variable $true hold value=true, variable($false) hold value=false. Now check the datatype using var_dump( ) function. Output will in boolean form: bool(true) bool(false)

Compound(Multiple values in single variable)

  1. Array
  2. Object

Array Data type



Output array(5) { [0]=> int(10) [1]=> int(20) [2]=> int(30) [3]=> int(40) [4]=> int(50) }

In the above example Variable( $arr) hold values an array . Now we want to print the first element of an array. Then we pass variable($arr) name with index value[0], fetch the first element corresponding to the index value. Output will 10

Object Data type

class Demo

public function show()


echo "This is show method<br/>";



$obj= new Demo();





Output object(Demo)#1 (0) { }

Special Data types

  1. Null
  2. Resource

Null Data type

The special Data type "NULL" represents a variable with no value.



Output NULL

Resource Data Type

The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP. A common example of using the resource data type is a database call. for eg:


$con = mysqli_connect("localhost","root","","users");


The function will return a resource type data to be stored into $con variable.

Some Predefine functions to Check data type

=> is_int( ) : Check given value is integer or not

=> is_float( ) : Check given value is float or not

=> is_numeric( ) : Check given value is either integer or float

=> is_string( ) : Check given value is string or not

=> is_bool( ) : Check given value is Boolean or not

=> is_array( ) : Check given value is array or not

=> is_object( ) : Check given value is object or not

=> is_null( ) : Check given value is null or not

Check if given variable holds a integer type of value then print the sum otherwise show error message

$x = 1000;
$y = 500;
if(is_int($x) && is_int($y))
 $sum = $x + $y;
echo "sum = ".$sum;
echo "both number must be integer";

Output sum = 1500

In the above example Create two variable $x hold value=100, $y hold value=500, now execute if..else condition. We pass is_int( ) function inside if condition, to check the value is integer or not , if yes statement is execute and print the sum of two values. if not else statement is execute show an error message.

Popular Tutorials

PHP Tutorial PHP Introduction For Beginners PHP Advantage PHP Software Requirement PHP WAMP Installation PHP XAMPP Installation PHP Syntax PHP Comments PHP Variables PHP $ and $$ Variables PHP Super global variables How to swap two numbers in PHP PHP Constant PHP:Magic Constant echo and print in PHP PHP Data Types PHP Form GET Method POST Method Form action PHP extract() Function PHP Display result in Textbox Multiple submit buttons php different actions Add two textbox values and display the result in third textbox PHP Operators PHP Arithmetic Operators PHP Assignment Operators PHP Comparative Operators PHP Logical Operators PHP Operators Precedence PHP Conditional Statement PHP if Statement PHP if-else Statement PHP if elseif else Statement PHP Switch Statement PHP calculator Profit and Loss What is Loop PHP For Loop Print star pattern in PHP Create table using for loop in PHP PHP While Loop PHP Do While Loop PHP Foreach Loop PHP String PHP String Functions PHP empty() function PHP str_split() Function PHP Numeric Functions PHP Tutorial PDF For Beginners PHP isset function PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP explode() Function PHP fprintf() Function PHP get_html_translation_table() Function PHP hebrev() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function