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:

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

<?php
/* DataTypeSamples.php
#- Copyright 2003 (c) 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: "
   .$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:

herong> \php\php DataTypeSamples.php

 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:?>

Table of Contents

 About This Book

 Introduction and Installation of PHP

 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

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 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

 Managing File Upload

 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

 Parsing and Managing HTML Documents

 Configuring and Sending Out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB