PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Data Types Supported in PHP

This section describes all 8 data types supported in PHP with a tutorial example showing how to use them: boolean, integer, float, string, array, object, resource, null.

PHP supports 8 data types:

  • boolean: It has only two literal values: 'true' and 'false'. I am not sure what is the storage size of a boolean data item.
  • integer: It is stored as signed integers with 32 bits. That means an integer data item can hold an integer value in the range of -2,147,483,648 and 2,147,483,647. Integer literals have 3 forms: decimal, octal (prefixed with '0'), and hexadecimal (prefixed with '0x').
  • float: It is stored as IEEE double floating point numbers with 64 bits. Float literals have the standard floating point number form.
  • string: It is a sequence of 8-bit characters. String literals have 3 forms: single quoted, double quoted, and heredoc syntax. These forms are very similar to Perl.
  • array: It stores an ordered map of pairs of keys and values. This is quite different than the array type used in other languages. Arrays in PHP are similar to associate arrays in Perl.
  • object: It stores an instance of a class.
  • resource: It represents an external resource like a file, or a database connection.
  • null: It represents a status of a variable in which no value is assigned the variable. Null type has only one literal value: 'null'.

Here is a tutorial sample PHP script that shows you all 8 data types:

<?php /* DataTypeSamples .php
* Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
*/

$isValid = '1'==1; // boolean data type
print "\n Boolean data type - '1'==1 evaluation result: ".$isValid;

$yearDays = 365; // integer data type
print "\n Integer data type - Days in a year: ".$yearDays;

$equatorLength = 3.14159*2*6371; // float data type
print "\n Float data type - Length of the equator: ".$equatorLength;

$greetingMessage = "Hello Herong!"; // float data type
print "\n String data type - Greeting message: ".$greetingMessage;

$monthDays = array("Jan"=>31,"Feb"=>28,"Mar"=>31); // array data type
print "\n Array data type - Days in each month: "
   .$monthDays['Jan'].", ".$monthDays['Feb'].", ".$monthDays['Mar']
   ."...";

$currentTime = new DateTime(); // object data type
print "\n Object data type - Current time: "
   .$currentTime->format(DATE_ATOM);

$currentDirectory = opendir("."); // resource data type
print "\n Resource data type - First item in the current directory: "
   .readdir($currentDirectory); 
closedir($currentDirectory);

$nothing = null; // null data type
print "\n Null data type - Null means nothing:".$nothing;
?>

If you run this sample script, you should get:

 Boolean data type - '1'==1 evaluation result: 1
 Integer data type - Days in a year: 365
 Float data type - Length of the equator: 40030.13978
 String data type - Greeting message: Hello Herong!
 Array data type - Days in each month: 31, 28, 31...
 Object data type - Current time: 2005-02-12T21:19:47-05:00
 Resource data type - First item in the current directory: .
 Null data type - Null means nothing:?>

Last update: 2005.

Sections in This Chapter

Data Types Supported in PHP

Data Literals Supported in PHP

Data Literals Examples for Integer, String and Other Data Types

Overflow of Integer and Float Values

Dr. Herong Yang, updated in 2009
Data Types Supported in PHP