PHP Variable Types

PHP variables are loosely-typed, so a variable’s type isn’t really defined until you assign a value to it. Prior to that, all variables have the special type ‘null’, which is just a way of saying no type and no value. Once you give a variable a value, it also gets a type. PHP supports 7 types, besides null, but for most of this title, we’re really only going to look at 4 of them:

int – these are your counting numbers
string – any sequence of characters inside single or double quotes
array – a set of things that can be referenced individually or as a group
boolean – true or false

In addition to those types, PHP also supports floats, objects, and resources and these get covered in detail later in this tutorial. But those first 4 types are the most important to learn here at the beginning, and will probably make up more than 90% of the variables you use in your everyday coding, at least for a while.

In order to determine a variable’s type, PHP provides a special function called ‘gettype’. Here it is in action:

echo gettype($var1); //assuming $var1 isn’t defined anywhere, this will print ‘NULL’

$var1 = ‘I am a string’;
echo gettype($var1); //prints ‘string’

$var1 = 55;
echo gettype($var1); //prints ‘int’

$var1 = array(55, 60);
echo gettype($var1); //prints ‘array’

$var1 = true;
echo gettype($var1); //prints ‘boolean’

{show output}

Now, if you have a variable of one type, but you need to use it a variable of a different type, you can do what’s called a cast. You do this by putting the desired variable type in parenthesis in front of the variable you want to convert. Here’s how it works.

Suppose I have a variable that’s an int:

$var1 = 56;

If I cast it to a string, it becomes:

$var2 = (string)$var1; //now the string ’56’

It’s now a string, made up of the characters 5 and 6. This is significant because text is stored differently than ints. They might print out the same on the screen, but to the server, it sees an int as the binary representation of the number 56. With strings though, it sees a separate binary representation for each character, and these have to be linked together in some way that preserves information about those characters’ order. So it takes a bit more memory to store the string ’56’ than the int 56.

If you want to cast your variable as an array, here’s what you do:

$var2 = (array)$var1;

In this case, it just takes $var1 and makes it the first value in a new PHP array, and assigns that array to $var2. We talk more about arrays in another video, but basically they’re just a way of holding sets of data together. And doing a type cast like this just makes a set with one item – the variable being cast.

Moving on, if you want to cast your variable as a boolean, you just do this:

$var2 = (bool)$var1;

-or-

$var2 = (boolean)$var1;

When converting an int to a boolean, in this case you’ll get true. In fact any int other than 0 will convert to true. Besides the int 0, other values that will convert to false are an empty string, an empty array, and a null. Everything else converts to true.

As far as converting things to ints, all you need to do is:

$var2 = (int)$var1;

or

$var2 = (integer)$var1;

PHP knows that int and integer mean the same thing. If you convert a boolean to an int, a value of true becomes 1, and a value of false becomes 0.

$var1 = true;
$var2 = (int)$var1; //$var2 is now 1

If you convert an array to an int, if the array is empty you get a 0, and otherwise you get a 1;

$var1 = array(56, 80);
$var2 = (int)$var1; //$var2 is now a 1

If you convert a string to an int, then if that string begins with a number, the int you convert to will have that number as its value

$var1 = ‘340,339,234’;
$var2 = (int)$var1; //$var2 is now 340

Notice that even though the number is written out as a string with commas representing the thousands and millions places, because those commas are non-numeric, the integer conversion stops at the first comma and only gives back the first 3 digits before the comma. And if the string doesn’t start with a numeral, even if there are numbers later in it, the converted value is a 0. One exception, by the way, is if the string starts with spaces followed by numbers. If that happens, the spaces get ignored and the numbers after them are used to create the integer.

Converting to a string from a boolean is simple. If your boolean is true, you get the string ‘true’. If your boolean is false, you get the string ‘false’.

Converting an array to a string is even simpler. No matter what the value of your array, whether it’s empty or not, the string representation is always the capitalized word ‘Array’.

February 06 2010 02:35 pm | Basics and PHP Programming Basics and PHP tutorial scripts

Comments are closed.