Introduction to PHP – Part2

advertisement

advertisement

Data Types:

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.

Example# 1:

$flag = True; // assign the value TRUE to $flag
Echo $flag; // Output will be 1, which means TRUE

Integer:

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.

Example# 2

$decimal = 4532; // positive decimal number
$negatige = -320; // negative decimal number
$Octal = 770; // octal number (equivalent to 504 decimal)
$Hex = 0x3F; // hexadecimal number (equivalent to 63 decimal)

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.

Hello World

Example# 4

$myString="Hello World";
echo $ myString;

The output of the above code will be

Hello World

A string can be specified in four ways.

  • Single quoted
  • Double quoted
  • heredoc syntax
  • now doc syntax

We will discuss string in detail in following tutorials.

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.