variables and PHP part 2

In part 2 we are going to deal with Data Types and Type Casting amongst other things.

Data Types

These are the data types which uses .

  • string ( text )
  • integer (numeric)
  • double(numeric)
  • array
  • object
  • unknown type

As you can see there are not as many data type are there are in some other languages , this in fact is a help to many a programmer . As a programmer you do not worry about setting these data types , PHP does it for you.


This is the data type that stores text , this can be single characters , words and even complete sentences . How many strings do you think we have below .

$mywages = "not much";
$intloan = "3000";

If you said one you are wrong , if you said two give yourself a pat on the back . remember we enclosed the number with quotation marks so PHP treats this as text information .


This means adding one string to another . If you are coming from another programming language this may (will) catch you out from time to time . In PHP we use the period ( . ) to concatenate strings.For example

$firstname = "Bill";
$secondname = "Gates";
echo $firstname . $secondname;

would print on the screen BillGates

Numeric Values

There are 2 different types of numerical data types in PHP , integers and doubles. Integers are basicly whole numbers and doubles are fractional (floating point ) numbers . Here are some examples .

$firstint = 9;
$firstdouble = 23.67;
$secondint = - 76;
$seconddouble = -1239.7432;

The values these types can hold is platform specific but for example the range for an integer is commonly -32768 to 32767 on Windows machines.

Type Casting

As you are aware PHP automatically decides what data type but you do have the option of specifying this yourself . Here is how you can do this .

$luckynumber = 7;
$lucknumber = (string) $luckynumber;

This would give us a string rather than the integer we had previously.

Using gettype and settype functions

These are two functions provided with PHP which help us to determine the data type and set the data type respectively , lets look at examples of both . Firstly settype which will display the data type of the variable

$myname = "Iain";
echo gettype($myname);

Now for settype , this allows you to set the data type . The first parameter is the actual variable , the second is the data type you require) . In this example we set the data type and then use gettype to show it has changed.

$wages = 1000;
settype($wages, "string");
echo gettype($wages);