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) 2015, HerongYang.com, All Rights Reserved.
$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: "
$currentDirectory = opendir("."); // resource data type
print "\n Resource data type - First item in the current directory: "
$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: 2015.
Table of Contents
About This Book
Introduction and Installation of PHP 7.0
PHP Script File Syntax
►PHP Data Types and Data Literals
►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
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Arrays - Ordered Maps
Configuring and Sending out Emails
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Introduction of Class and Object
Integrating PHP with Apache Web Server
PDF Printing Version