All variables in PHP start with a dollar sign ($). Unlike many other languages, PHP does not have different types of variable for integers, floating-point numbers, arrays, or Booleans. They all start with a $, and all are interchangeable. As a result, PHP is a weakly typed language, which means you do not declare a variable as containing a specific type of data; you just use it however you want to.

Save the code in Listing 29.1 into the script ubuntui.php.

Listing 29.1. Testing Types in PHP


$i =


$j =


$k =

"Hello, world";


$i + $j;


$i + $k;


To run that script, bring up a console and browse to where you saved it. Then type this command:

$ php ubuntui.php

If PHP is installed correctly, you should see the output 2010, which is really two things. The 20 is the result of 10 + 10 ($i plus $j), and the 10 is the result of adding 10 to the text string Hello, world. Neither of those operations are really straightforward. Whereas $i is set to the number 10, $j is actually set to be the text value "10", which is not the same thing. Adding 10 to 10 gives 20, as you would imagine, but adding 10 to "10" (the string) forces PHP to convert $j to an integer on-the-fly before adding it.

Running $i + $k adds another string to a number, but this time the string is Hello, world and not just a number inside a string. PHP still tries to convert it, though, and converting any non-numeric string into a number converts it to 0. So, the second echo statement ends up saying $i + 0.

As you should have guessed by now, calling echo outputs values to the screen. Right now, that prints directly to your console, but internally PHP has a complex output mechanism that enables you to print to a console, send text through Apache to a web browser, send data over a network, and more.

Now that you have seen how PHP handles variables of different types, it is important that you understand the selection of types available to you.

Type Stores integer Whole numbers; for example, 1, 9, or 324809873

float Fractional numbers; for example, 1.1, 9.09, or


string Characters; for example, "a", "sfdgh", or "Ubuntu



Stores boolean

True or false array

Several variables of any type object

An instance of a class resource

Any external data

The first four can be thought of as simple variables, and the last three as complex variables. Arrays are simply collections of variables. You might have an array of numbers (the ages of all the children in a class); an array of strings (the names of all Wimbledon tennis champions); or even an array of arrays, known as a multidimensional array. Arrays are covered in more depth in the next section because they are unique in the way in which they are defined.

Objects are used to define and manipulate a set of variables that belong to a unique entity. Each object has its own personal set of variables, as well as functions that operate on those variables. Objects are commonly used to model real-world things. You might define an object that represents a TV, with variables such as $currentchannel (probably an integer), $SupportsHiDef (a Boolean), and so on.

Of all the complex variables, the easiest to grasp are resources. PHP has many extensions available to it that allow you to connect to databases, manipulate graphics, or even make calls to Java programs. Because they are all external systems, they need to have types of data unique to them that PHP cannot represent using any of the six other data types. So, PHP stores their custom data types in resourcesdata types that are meaningless to PHP but can be used by the external libraries that created them.

Arrays are one of our favorite parts of PHP because the syntax is smart and easy to read and yet manages to be as powerful as you could want. There are four pieces of jargon you need to know to understand arrays:

• An array is made up of many elements.

• Each element has a key that defines its place in the array. An array can have only one element with a given key.

• Each element also has a value, which is the data associated with the key.

• Each array has a cursor, which points to the current key.

The first three are used regularly; the last one less so. The array cursor is covered later in this chapter in the section "Basic Functions," but we will look at the other three now. With PHP, your keys can be virtually anything: integers, strings, objects, or other arrays. You can even mix and match the keys so that one key is an array, another is a string, and so on. The one exception to all this is floating-point numbers: You cannot use floating-point numbers as keys in your arrays.

Was this article helpful?

0 0

Post a comment