PHP DataTypes: Learn With Example


Objective

What are DataTypes?
PHP Datatypes with Programming Example

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

  1. String
  2. Integer
  3. Float (floating point numbers – also called double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. 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 Move2Code.com";
  echo $str;
?>
Output
You are learning PHP on Move2Code.com

Escape Sequence

Escape sequence are used for inserting special characters in a string. There are following escape sequences are available in PHP.

  1. \n = For New Line (Equivalent to <br /> tag)
  2. \r = For Carriage Return
  3. \t = For Tab Space (Equivalent to 8 white spaces)
  4. \$ = For $ sign
  5. \" = For single Double Quotes (").
  6. \\ = For single Back Slash (\)
Notes:
Several Escape Sequences don't work on the browser because browser respects pure HTML tag over PHP escape sequences. So you can use <br /> tag instead of \n.

Example
<?php
  $str = "You are <br /> learning PHP on \"Move2Code.com\"";
  var_dump($str);
?>
Output
string(46) "You are
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.

Example
<?php
  $num=123;
  var_dump($num);
?>
Output
Int(123)
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
float(123.44)
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
bool(true) bool(false)
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
array(7) { [0]=> string(6) “Sunday” [1]=> string(6) “Monday” [2]=> string(7) “Tuesday” [3]=> string(9) “Wednesday” [4]=> string(8) “Thursday” [5]=> string(6) “Friday” [6]=> string(8) “Saturday” }
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
Book Name: Java
object(books)#1 (1) { [“bookname”]=> string(4) “Java” }
Explanation:

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
NULL
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
object(mysqli)#1 (19) { [“affected_rows”]=> int(0) [“client_info”]=> string(79) “mysqlnd 4.0.10-dev – 20123007 – $Id: b5c5906d45j76t90732a89k341f3888t3547yt54 $” [“client_version”]=> int(40010) [“connect_errno”]=> int(0) [“connect_error”]=> NULL [“errno”]=> int(0) [“error”]=> string(0) “” [“error_list”]=> array(0) { } [“field_count”]=> int(0) [“host_info”]=> string(25) “Localhost via UNIX socket” [“info”]=> NULL [“insert_id”]=> int(0) [“server_info”]=> string(23) “3.1.11-0ubuntu3.23.53.6” [“server_version”]=> int(26030) [“stat”]=> string(132) “Uptime: 5504 Threads: 1 Questions: 9 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 26 Queries per second avg: 0.001” [“sqlstate”]=> string(5) “00000” [“protocol_version”]=> int(10) [“thread_id”]=> int(9) [“warning_count”]=> int(0) }

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.

Back Next