Introduction to PHP – Part2

PHP supports eight primitive types; four scalar types two compound and two special types. In this section we will discuss scalar types; boolean, integer, float and string. Other four array, object, resource and null will be discussed in the later sections.

Boolean:

To define boolean variable we can specify TRUE or FALSE, both are case insensitive. The size of the Boolean variable is one byte.

According to PHP manual an integer is a number of set Z = {…, -2, -1, 0, 1, 2, …}. In general integer is a positive or negative number without decimal point. Integer can be specified in decimal, hexadecimal or octal notation. The sign (+ or -) is optional. If the integer is precede by number 0(zero), it is octal notation and 0X is hexadecimal notation. The size of integer is platform dependent and maximum size is four bytes. If a number greater than integer range is encountered, PHP interpret it as float.

Note that there is no integer division is available in PHP, the result is always float.

Float:

Floating point numbers are the real number with decimal point. They are also known as doubles. The size of float is platform dependent, the maximum size is eight bytes with a precision of roughly 14 decimal digits.

Example# 3

$x = 5.362; // floating point number
$y = 2.3e2; // can also be expressed in e notation
$z = 2E-10; // another form of float representation

String:

A string variable is used to store array of characters or character strings. For instance, if we need to store characters “ABCD” in a variable, string is required. A string can also contain numbers, space and special characters and should enclose by quote. PHP supports a string of any size, the limit depends upon the memory of the computer on which PHP is running.

The following example creates a string variable called $myString, assign string “Hello World” to the string and finally prints $myString to the monitor.