PHP Data Types

A variable's type refers to the type of data stored in it. PHP has the following data types

 

Integer
Used for whole numbers
Float
(also called Double)
Used for real numbers
String
used for strings of characters
Boolean
Used for true or false values
Array
Used to store multiple data items
Object
Used in Object Oriented Programming

A variables type is determined automatically by what sort of data is entered into it.

 

A Note About Strings

When we talk about strings, we are basically talking about text. A string is a collection of alphanumeric characters (i.e. letters and numbers) strung together (to make words, sentences etc).

When we join strings together in PHP we use a period or fullstop (.) to do so. (This is called string concatenation). For example

echo $mon_qty.'is the number of monitors you ordered<br />';

This joins two strings together. The first part is the variable $mon_qty; the value typed into the monitor text field will be displayed. The second part (after the period) is in single quotes and will be displayed as written (this is called a "literal" as opposed to a variable).

If someone typed "5" into the monitor textfield on the order form, then

5 is the number of monitors you ordered

should be displayed. Notice the <br /> tag after the text. This means the next text to be displayed will start on a new line. The HTML tag must be placed inside the single quotes so it will be echoed to the browser.

 

Constants

A constant stores a value like a variable, but the value can be set only once and remains fixed thereafter. For our computer supply store we are going to add constants for the prices of our stocked items. Add the following code to the end of the PHP script (after the last echo statement).


define('hd_price', 85);
define('mon_price', 450);
define('pr_price', 112);

This introduces three constants into the script and sets their values to the numbers next to them, e.g. 'hd_price is set to 85, and will rmain that value for the entire script.