PHP Basics series – Variable types and outputting

So far we have discussed a bit about coding and standards, a bit more about starting with PHP and now we will discuss about PHP variable types and outputting (displaying on the screen).

Some developers say that PHP have a “hybrid” variable type or, by what they mean, PHP does not carry a true primitive type since you don’t need to specify it when declaring a variable.

In some ways that’s true, you really don’t need to specify what is the variable type when declaring a variable, but that’s most because PHP engine already detects that for you automatically and it will also make the necessary conversions (cast) when necessary.

PHP has, as primary variable types:

Scalar Types

$stringType = ‘string’;
$integerType = -10, 10;
$floatType = -10.9, 10.9;
$booleanType = TRUE, FALSE, 1, 0; // in this case 1 and 0 are also interpreted as boolean

Dynamic Types (compound)

$arrayType = array();
$objectType = new DirectoryReader(‘./’);

Special Types

$resourceType = fopen(‘file.txt’);
$nullType = NULL;

As seen above there is no need to declare the variable type to determine that it is a string, boolean, integer, float, etc. When working with several types at same time, you can cast them to become something else or, depending on the operation, PHP will do that for you.

Here is a nice example of when PHP converts something to get the result.

$result = 9 + ‘9’ – 1;

The result above will be the sum of 9 plus the integer equivalent of 9 less 1. If we were dealing with strings, the same will happen.

$result = ‘The sum of 9 + 9 is ‘ . (9+9) . ‘!’;

In this case PHP will first sum and them convert the result to a string so it can be concatenated with the rest of the string.

There is no real mystery in declaring a variable, but there are some small rules:

  1. A variable name will never start with a number, always with a letter or underscore
  2. In order to PHP to know that it’s a variable that is dealing with, the dollar sign ($) must precede all variable names
  3. The declaration of a variable must receive some value even if it’s null. This type can be later changed to the correct one with value.

Considering this all the variable declarations above are correct, but to help to get it right, here are some true examples of incorrect variable declarations.

$1234Variable = ‘1234’;
variable = ‘test’;

The correct format for those examples above are:

$_1234Variable = ‘1234’;
$variable = null;
$variable = test;

Keep the 3 golden rules of variable declaration in mind and every time it will be right. Whenever in doubt that the variable type is really the one being looked for, just do a cast. Here are some examples;

$variable = ‘1234’;
$integerVariable = (int) $variable;
$floatVariable = (float) $variable;

Besides variable declaration the second thing most done in a PHP script is outputting results on the screen, or printing.

For most cases the printing is done by using one of the following:

  1. echo to print any scalar value on the screen, or how the manual mention it, to echo something on the screen
  2. print to print any scalar value on the screen. It’s pretty much the same as echo, but with the difference of being a function, so you can grab a result from it (that will always be 1).
  3. print_r to print arrays mostly and some objects that support the array iterator

There are more ways to output something on a document, but for now, that’s all is necessary to be known.

To wrap up this article here is a full example with correct variable declarations and the respective output.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
Page Title</title>
<!– other head data such as CSS styling and JS scripts –>

$_string = ‘ This is a string ‘;
$_interger = 7; // for luck
$_boolean = TRUE; // it could also be FALSE, 1 or 0
$_array = array($_string,$_interger,$_boolean);

echo $_string . ‘<br />’; // this can be also done whithout concatenation by closing the PHP tag, placing the br tag on place and opening again if needed to continue with the PHP script – look bellow

print $_interger . ‘<br />’;

print ($_boolean . ‘<br />’);

The document would result:

This is a string

[0] => ‘This is a string’
[1] => 7
[2] => TRUE

Play a bit with it and do some testing, check results. Next article the focus will be on the Array which is maybe the most important type in the whole PHP engine.

Have fun.

About mcloide

Making things simpler, just check: View all posts by mcloide

3 responses to “PHP Basics series – Variable types and outputting

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: