Searching...
Wednesday 12 June 2013

PHP Basics - Part 4 :: PHP’s Supported Data Types


A datatype is the generic name assigned to any data sharing a common set of characteristics. Common data types include Boolean, integer, float, string, and array. All of the acailable data types are discusse next:

Scalar Data Types

Scalar data types are used to represent a single value. Several data types fall under this category, including Boolean, integer, float, and string.

Boolean

The Boolean datatype is named after George Boole (1815–1864), a mathematician who is considered to be one of the founding fathers of information theory.

The Boolean data type represents truth, supporting only two values: TRUE and FALSE (case insensitive). Alternatively, you can use zero to represent FALSE, and any nonzero value to represent TRUE.
A few examples follow:

$alive = false; // $alive is false.
$alive = 1; // $alive is true.
$alive = -1; // $alive is true.
$alive = 5; // $alive is true.
$alive = 0; // $alive is false.

Integer

An integer is representative of any whole number or, in other words, a number that does not contain fractional parts. PHP supports integer values represented in base 10 (decimal), base 8 (octal), and base 16 (hexadecimal) numbering systems, although it’s likely you’ll only be concerned with the first of those systems. Several examples follow:

42 // decimal
-678900 // decimal
0755 // octal
0xC4E // hexadecimal

  • On 32-bit platforms, integer values can range from -2,147,483,648 to +2,147,483,647 for PHP version 5 and earlier. PHP 6 introduced a 64-bit integer value, 64-bit platforms usually have a maximum value of about 9E18.
  • To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

Float

Floating-point numbers, also referred to as floats, doubles, or real numbers, allow you to specify numbers that contain fractional parts. Floats are used to represent monetary values, weights, distances, and a whole host of other representations in which a simple integer value won’t suffice. PHP’s floats can be specified in a variety of ways, several of which are demonstrated here:

4.5678
4.0
8.7e4
1.23E+11

String

Simply put, a string is a sequence of characters treated as a contiguous group. Strings are delimited by single or double quotes, although PHP also supports another delimitation methodology, which will be introduced in the later lectures.
The following are all examples of valid strings:

"PHP is a great language"
"whoop-de-do"
'*9subway\n'
"123$%^789"

PHP treats strings in the same fashion as arrays (discussed next), allowing for specific characters to be accessed via array offset notation. For example, consider the following string:

$color = "maroon";

You could retrieve a particular character of the string by treating the string as an array, like this:

$parser = $color[2]; // Assigns 'r' to $parser

Compound Data Types

Compound data types allow for multiple items of the same type to be aggregated under a single representative entity. The array and the object fall into this category.

Array

It’s often useful to aggregate a series of similar items together, arranging and referencing them in some specific way. This data structure, known as an array, is formally defined as an indexed collection of data values. Each member of the array index (also known as the key) references a corresponding value and can be a simple numerical reference to the value’s position in the series, or it could have some direct correlation to the value. For example, if you were interested in creating a list of U.S. states, you could use a numerically indexed array, like so:

$state[0] = "Alabama";
$state[1] = "Alaska";
$state[2] = "Arizona";
...
$state[49] = "Wyoming";

But what if the project required correlating U.S. states to their capitals? Rather than base the keys on a numerical index, you might instead use an associative index, like this:

$state["Alabama"] = "Montgomery";
$state["Alaska"] = "Juneau";
$state["Arizona"] = "Phoenix";
...
$state["Wyoming"] = "Cheyenne";

Don’t be too concerned if you don’t completely understand these concepts right now. I'll be evaluating arrays in the later lectures completely.

Note: PHP also supports arrays consisting of several dimensions, better known as multidimensional arrays.

Object

The other compound datatype supported by PHP is the object. The object is a central concept of the object-oriented programming paradigm. Unlike the other data types contained in the PHP language, an object must be explicitly declared. This declaration of an object’s characteristics and behavior takes place within something called a class.

Here’s a general example of a class definition and subsequent invocation:

class Appliance
{
private $_power;
function setPower($status)
{
$this->_power = $status;
}
}
...
$blender = new Appliance;

A class definition creates several attributes and functions pertinent to a data structure, in this case a data structure named Appliance. There is only one attribute, power, which can be modified by using the method setPower(). Remember, however, that a class definition is a template and cannot itself be manipulated. Instead, objects are created based on this template. This is accomplished via the new keyword. Therefore, in the last line of the previous listing, an object of class Appliance named blender is created. The blender object’s power attribute can then be set by making use of the method setPower():

$blender->setPower("on");

Improvements to PHP’s object-oriented development model are numerous in PHP 5.


0 comments:

Post a Comment

 
Back to top!