Variables & Data Types in PHP

[Index]

Variables are found at the heart of every programming language. If this is your first forray into programming, you should know that a variable is an identifer put aside by the programmer to hold information. This will reserve a place in the computers memory where this information is kept.

In many programming languages the programmer must declare the variable before using it, in order to register it and set aside space for it in the computers memory. This is not necessary in PHP. PHP will automatically register the variable the first time it encounters it.

In PHP all variables are identified by using the $ sign as a prefix. Once we have a variable we have to assign a value to it. In PHP we do this using the equals sign(=).In the following example we identify a variable $myBard and assign a value to it of "William Shakespeare". We then print it out.

Example 1

 1<html>
 2<title>String Variable</title>
 3 <?
 4 $myBard="William Shakespeare";
 5 print($myBard);
 6 ?>
 7</html>

Note that on line5 the variable $myBard is not quoted!

Rules for naming variables

All you have to do to name a variable in PHP is stick a $ in front of it! Other rules include:

If you follow these rules then you can call your variables anything you want $myBard or $_hwxojn1234rljhmnJ_HJNKmjdh! But see below!

You should also know that PHP maintains a set of variables that it passes in the HTTP header. These are all uppercase and begin $HTTP_***_****. For this reason it is suggested that you do not use ALLCAPS for your variable names, in order that PHP variables will stand out in your code.

Case Sensitivity

In PHP case is important! $myBard is not the same variable as $mybard.

Sensible Naming Conventions

Here are a few rules and conventions that will make you life much easier if you follow them.

Give your variables meaningful names

If you varible contains someones last name then call it that! i.e.$lastname or $lname.

Use a 'visual' convention for long variables

Long variable names can be difficult to read, for example $stmaryshospitalcostcenter. This is much easier to read if it is broken up with Uppercase letters, e.g.$stMarysHospitalCostCenter (this convention is called 'camelBack' notation) or $StMarysHospitalCostCenter (this convention is called 'InitialCaps' notation), or $st_marys_hospital_costcenter (this convention is called 'under_score' notation). It is a good idea to pick a style of your own and stick with it, although if you work for a 'shop' you may be told which style to use!

Prefix your variables with a data type

PHP is not a strongly typed language (see next section), but that doesn't mean that the variables do not have types. In many cases it is a good idea to prefix the variable with the type of variable it contains, e.g. $strSQL for a string, $boolWindowState for a boolean, $intUnits for an integer, $objXMLtree for an object, etc. This is just all part of good programming practices.

Data Types

For those new to programming, you should be aware that variables come in different forms of data types. For the most part, in PHP programs we do not have to worry about this. We will take a look in a little detail at just two data types in this lesson, text and numbers.

String Data types

Any thing that can be put in quotes is a string. In Example 1 we assigned the string "William Shakespeare" to the variable $myBard. It is worth while noting that "999.99" is a string and not a number as far as the computer is concerned. To prove this we will show the following example. It uses the gettype() method which we will discuss below.

Example 2

  1<html>
  2<title>String Variable II</title>
  3 <?
  4 $myNumericString="999.99";
  5 print($myNumericString);
  6 print(" is of type ");
  7 print gettype($myNumericString) ;
  8 ?>
  9</html>
 

The above program will print out "999.99 is of type string", because indeed "999.99" is quoted, and is a textual representation of the number. Lets look at Numeric data types now.

Numeric Data types

You will possibly remember from high school math, that there were two types of numbers, integers and real numbers. Integers had no decimal point e.g. 999 is an integer, and real numbers had decimal points, e.g. 999.99 was a real number. In PHP real numbers are AKA doubles or floats.

In the following example we show how PHP automatically figures out whether the value we assign to a variable is a double or an integer.

Example 3

  1 <html>
  2 <title>Numeric Variables</title>
  3  <?
  4  $myInt=999;
  5  print($myInt);
  6  print(" is of type ");
  7  print gettype($myInt) ;
  8  $myDbl=999.99;
  9  print($myDbl);
  10 print(" is of type ");
  11 print gettype($myDbl) ;
  12 ?>
  13</html>
 

The Above program will print out "999 is of type integer999.99 is of type double "

Checking Data types

We have already done this in example 2 and 3! The gettype() function, takes as it's argument the variable that we want to check, and returns a string of the type of variable. The general syntax is:


  [$strVarType]=gettype([variable name])

And $strVarType will contain the string 'integer', 'string', 'double', etc.

Checking Specific Data types

It is also possible to check whether a variable is of a specific type using the following PHP methods. They all take the variable as an argument, and return a boolean, true if the variable is of the correct type, otherwise false.

For those new to programming, note that the following code uses a conditional clause from lines 6 - 9 which will be covered in the lesson 'loops and logic'

Example 3

  1 <html>
  2 <title>Discovering Variable Types</title>
  3  <?
  4  $myInt=999;
  5  print($myInt);
  6    if(is_integer($myInt))
  7     {print ("The variable is an integer");}
  8    else
  9     {print ("The variable is NOT an integer");}

  10 ?>
  11</html>
 
change $myInt to a non-integer value, and run it a again

Changing Data types

Some times we want to change data types. This process is tecnically known as 'casting' a data type. We can force PHP to convert one data type to another using the settype() method. Look at the following example:

  1  <html>
  2  <title>Casting a string</title>
  3  <?
  4   $myMix="123abc";
  5    echo($myMix);
  6    echo " is of type ";
  7    echo gettype($myMix);
  8    settype($myMix,"integer");
  9    echo($myMix);
  10   echo " is of type ";
  11   echo gettype($myMix);

  12 ?>
  13 </html>

This program will print "123abc is of type string123 is of type integer". Note that we have used 'echo' instead of 'print' for no particular reason!

In line 8 settype()takes two arguments, the variable name, and the type we want to covert it to, in this case 'integer'. In line 9 we print out the converted variable which is now just 123. settype() will read as far into a variable as it can. It will stop reading when it comes to a bit that cannot be interpreted as the required variable type. If $myMix had originally been 'abc123' then the result would br 0 as settype() would have stopped reading as soon as it reached the character 'a', as this could not be converted.

Other Data types

For the record here are the datatypes recognised by PHP

Summary

PHP hides much of the details of handling variables from us, we do not have to declare them specifically or keep track of their type. All we hafve to do is prefix the variable name with $. The down side of this is that if we mis-spell a variable in our code it may not be immediatly obvious, and some really strange things can happen in our program! So be very careful to check all spelling!


©Frank Boumphrey 2001