When you learn PHP for the first time, datatypes may not seem that important. You are okay to focus on learning to handle PHP variables. But as your programs become more sophisticated, you learn that PHP datatypes impact on how you affect different functions in your programs. In this PHP tutorial, we look at each of PHP's datatypes in turn and consider examples of how to implement them.
A datatype is the type of container that the variable is. In the general discussion on variables, you will note references to integers and strings. These are two kinds of containers. PHP has six kinds of datatypes that we will look at below.
A string is a set of alphanumeric characters that may also include symbols. A basic example is a sentence. All of these are strings:
$quote1 = "Outside of a dog, books are a man's best friend.";
$quote2 = "Inside of a dog, it's too dark to read.";
$source = "Mark Twain";
As in mathematics, an integer is a numeric value of the Whole Number system. These can be negative, postive, or zero (null). The following are examples of integers:
$day = "11";
$month = "11";
$year = "1918";
A float is a numeric value that is expressed with decimal values. The following are floats:
$pi = 3.14159265;
$e = 2.71828;
$c = 0.299792458;
Boolean values are non-numeric in nature. Boolean values represent the truth status of a value and can be either TRUE or FALSE. The following are booleans:
$condition = true;Booleans are often used to set and test conditions for execution.
$fiction = false;
A PHP array is a two-dimensional matrix of key-value pairs. If you had separate values of similar kind for each in a series of data, you might use long variable names as follows:
$entryTotal = 270;But this becomes unwieldy very quickly. Instead, one can use two separate arrays, $entry and $exit:
$entryRate = 18;
$entryTime = 15;
$exitTotal = 255;
$exitRate = 17;
$exitTime = 15;
$entry = array (You then reference array elements by putting the key of the array in squared brackets:
'total' = 270,
'rate' = 18,
'time' = 15
$exit = array (
'total' = 255,
'rate' = 17,
'time' = 15
echo $entry['total']; // Outputs 270Note that, given the nature of arrays, you cannot concatenate array elements.
echo $exit['rate']; // Outputs 17
An object is an instance of a class. A class is a collection of methods and attributes that define the behaviour of a set piece of PHP code. A class is a design layout or blueprint of code. An object is an instance of that code in action. As Jason Lengstorf mentions in PHP for Absolute Beginners:
"an object is to a class as a house is to a blueprint"Objects and classes fall under object-oriented programming. We look more at object-oriented PHP elsewhere on this site.
Given that PHP dynamically converts integers to strings, you may wonder why datatypes are important. PHP is not a strongly typed language so you can reuse variable handles to hold different kinds of data, but there are limits on what you can do with the data you store. Those limits are determined by the datatype.
For small programs where data is more or less static, those limits don't matter too much. But the more complex a program becomes, the more abstract the logical process becomes and the less certain one can be of the environment in which the program runs. In such cases, datatype errors can result in a program crashing.
|Install PHP||Basic PHP Configuration||PHP Configuration In-Depth|
|Learn PHP||PHP in HTML||PHP Keywords|
|PHP Expressions||PHP Variables||PHP Constants|
|Defined Constants||Constants Characteristics||PHP Datatypes|
|PHP Syntax Basics||PHP Loops|
|Install Apache||Basic Configuration||Apache In-Depth|
|Recommend Web Hosting||Customising Apache||PHP Hosting|
|Install MySQL||Basic MySQL Configuration||MySQL Configuration In-Depth|