- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Data Types
Since PHP is a loosely typed language, therefore whatever the value assigned to a variable, the variable becomes automatically of that type.
List of data types supported by PHP are:
- integer
- float (also called as double)
- string
- array
- boolean
- NULL
- object
Note - The gettype() function returns the data type of a variable or the value itself, specified as its parameter.
PHP integer Data Type
All non-decimal numbers between -2,147,483,648 and 2,147,483,647 are considered as an integer type in PHP. For example:
<?php echo gettype(5), "<BR>"; $x = 12; echo gettype($x), "<BR>"; $x = -48; echo gettype($x), "<BR>"; $x = 0; echo gettype($x), "<BR>"; ?>
The output produced by above PHP example is:
To get data type along with value/information, use var_dump() function. Here is an example:
<?php echo var_dump(5), "<BR>"; $x = 12; echo var_dump($x), "<BR>"; $x = -48; echo var_dump($x), "<BR>"; $x = 0; echo var_dump($x), "<BR>"; ?>
Now the output produced by this PHP example is shown in the snapshot given below:
PHP float Data Type
A number having a decimal point or a number in exponential form, is considered as a floating-point type in PHP. For example:
<?php echo var_dump(5.3); ?>
The output should be float(5.3)
PHP string Data Type
A text/value enclosed within a single or a double quote, is considered as a string type in PHP. For example:
<?php echo var_dump('13'), "<BR>"; echo var_dump("14.4"), "<BR>"; echo var_dump("codescracker.com"), "<BR>"; ?>
The snapshot given below shows the output produced by above PHP example on string data type:
In above output, the value inside string() indicates the number of character available in the string. For example, since there are only two characters available in the string '13', therefore the output was string(2) "13" for first statement. Similar thing goes with second and third statement. Use gettype() to print only the type of value/variable.
PHP array Data Type
The array is used when we need to store multiple values in a single variable. For example:
<?php $x = array("Python", 32, "PHP"); echo var_dump($x); ?>
The output is:
In above output, array(3) indicates that the variable $x is of array type with total of 3 elements available or defined in/to it. Now inside the curly braces, the first one, that is [0]=> string(6) "Python" indicates that at 0th index, the element available is of string type whose length is 6 and value is Python. Similar thing goes with second and third element.
Let me tell you again, if you want to print, only the data type of a variable or a value, then better to go with gettype(). And to get the data type along with other information, use var_dump(). For example, if I use gettype() in place of var_dump() in above example, then the output should be array
PHP boolean Data Type
The two values that are of boolean type are true and false. For example:
<?php echo gettype(true); echo gettype(false); ?>
You will get boolean as/on output for both the statement.
PHP NULL Data Type
A variable without value, considered as a variable with a NULL value, because NULL automatically gets initialized to a variable with no value at all. Also, we can initialize NULL to an already defined variable to empty the variable. Here is an example on NULL data type in PHP:
<?php $x = NULL; echo gettype($x); ?>
The output should be NULL
PHP object Data Type
<?php class myClass{ } $x = new myClass(); echo gettype($x); ?>
The output should be object. The detailed description about classes and objects are described in its separate tutorial.
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube