DataType defines the type of variables. Datatypes convert that variable according to stored value. For example, if you store "Hello PHP" in a variable, data type makes this variable string type and then you can run string operation on that variable.
There are following datatypes in PHP
String
Integer
Float (floating point numbers – also called double)
Boolean
Array
Object
NULL
Resource
1. PHP STRING DATATYPE
A String
is a sequence of character and can be stored in a variable inside double quotes ("") or single quotes("). In PHP you can store up to 2GB of string in a variable.
<?php $str="You are learning PHP on completecsharptutorial.com"; echo $str; ?>Output
Escape Sequence
Escape sequence are used for inserting special characters in a string. There are following escape sequences are available in PHP.
- \n = For New Line (Equivalent to
<br />
tag) - \r = For Carriage Return
- \t = For Tab Space (Equivalent to 8 white spaces)
- \$ = For $ sign
- \" = For single Double Quotes (").
- \\ = For single Back Slash (\)
<br />
tag instead of \n.
Example
<?php $str = "You are <br /> learning PHP on \"Move2Code.com\""; var_dump($str); ?>Output
learning PHP on "Move2Code.com"
2. PHP INTEGER DATATYPE
Integer DataType stores numeric value between -2,147,483,648 and 2,147,483,647. An integer variable doesn't store decimal values. var_dump(variable)
methods return the datatype of variable.
<?php $num=123; var_dump($num); ?>Output
3. PHP FLOAT OR DOUBLE DATATYPES
A float data type can contain a number with decimal point.
Example<?php $num=123.44; var_dump($num); ?>Output
4. PHP BOOLEAN DATA TYPE
Boolean Data Type contains only two values, true or false.
<?php $num=true; $sum=false; var_dump($num); echo "<br />"; var_dump($sum); ?>Output
5. PHP ARRAY DATA TYPE
An array contains multiple values in a single variable. An array can contain any value string, integer or float.
Example<?php $days=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); var_dump($days) ?>Output
6. PHP OBJECT DATA TYPE
An object is an instance of a class and can be created using the new keyword. An object is a data type which stores data and information on how to process the data.
Example<?php class books { public $bookname="Java"; } $book_obj=new books; print "<b>Book Name: </b>".$book_obj->bookname; print "<br />"; var_dump($book_obj); ?>Output
object(books)#1 (1) { [“bookname”]=> string(4) “Java” }
In the previous example, I have created a simple class books which has a public variable bookname that holds "Java". Later, I have created an object $book_obj
of class books and accessed its member variables.
7. NULL DATA TYPES
Null is a special type of data type that holds only one value Null. An empty variable or object is automatically assigned with Null. Simply understand, Null means nothing or empty.
Example<?php $num; var_dump($num); ?>Output
8. RESOURCE DATA TYPE
The Resource is a special type of datatype that holds the reference of external resources like a link of database connection or link of opened file.
Example<?php $link=mysqli_connect(localhost,"root","root"); var_dump($link); ?>Output
SUMMARY
In this chapter you learned about all the datatype in php with complete programming example. In the next chapter you will learn about PHP Operators.