Understanding PHP Variable Types

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
An Introduction to PHP VariablesPHP Constants


Purchase and download the full PDF and ePub versions of this PHP eBook for only $8.99


In this chapter we will look at the PHP integer, string, float and boolean variable types. Since the array and object types are slightly more complex entities we will devote subsequent chapters to them later in the book (see PHP Arrays and PHP Object Oriented Programming).


Contents


The PHP Integer Variable Type

Integer variables are able to hold a whole number in the range of -2147483648 to 2147483647. Negative values can be assigned by placing the minus (-) sign after the assignment operator and before the number. If the value assigned to an integer type variable moves outside the supported range, either via assignment or mathematical calculation, the variable type is automatically converted to a floating point type.

The following examples assign integers to variables:

<?php

$myInteger = 10;

$myNegative = -13457231;

?>

Integer values can be specified in Octal by prefixing the value with a zero '0':

$myoctInteger = 0456;

Similarly, hexadecimal values are pre-fixed with 0x:

$myHexInteger = 0x5EF3;

The PHP Float Variable Type

Floating point variables contain numbers that require the use of decimal places. In addition, float variables can store whole numbers up to higher values than the integer variable type (such as 1.067, 0.25, 423454567098, 84664435.9576). Floating point variable creation and initialization examples are as follows:

<?php

$myFloat = 9234.98;

$myOtherFloat = 9547894367.987483701

?>

The PHP Boolean Variable Type

PHP boolean type variables hold a value of true or false and are used to test conditions such as whether some part of a script was performed correctly. We will look at using boolean values in greater detail when we look at PHP Flow Control and Looping, with particular regard to the if statement. It is useful to know that the true and false values are actually represented internally by PHP boolean values 1 and 0 respectively, though it is important to be aware that boolean 1 and 0 are not the same as integer values 1 and 0.

The PHP String Variable

The string variable type is used to hold strings of characters such as words and sentences. In addition to providing mechanisms for creating and changing entire string variable values, PHP allows you to extract and change parts of a string value.

A string can be assigned to variable either by surrounding it in single quotes (') or double quotes ("). If your string itself contains either single or double quotes you should use the opposite to wrap the string:

<?php

$myString = "A string of text";

$myString2 = 'Another string of text';

$myString3 = "This string contains 'single quotes'";

$myString4 = 'This string contains "double quotes"';

?>

You can also escape quotes in your string by preceding them with a backslash (\), especially useful if your string contains both single and double quotes of its own that would otherwise confuse the PHP pre-processor:

<?php

$myString3 = 'This string contains \'single quotes\'';

$myString4 = "This string contains \"double quotes\" and \'single quotes\'";

?>

Double quoted strings also allow the insertion of special control sequences that are interpreted to have special meaning for the PHP pre-processor (such as a tab or new line). The following table outlines the various control sequences and their respective descriptions:

Control SequenceDescription
\nNew line
\rCarriage Return
\tTab
\\Backslash Character
\"Double quotation mark
\$Dollar sign (prevents text from being treated as a variable name)
\034Octal ASCII value
\x0CHexadecimal ASCII Value

As an example, we can declare a string variable which contains a tab and a new line character as follows:

<?php
$myString = "This is a line of Text\nandthis is another line with a tab here \t for us.";
?>

Extracting and Writing String Fragments

Once we have defined a string variable we can extract or make changes to individual characters in the string using what is termed {x} notation, where x represents the index into the string of the character we wish to view or change. Before we look at an example, it is important to keep in mind that the index into the string is zero based. By this we mean that the first character of the string is in position 0, not position 1. For example, to change the first and last characters of a string variable:

<?php

$myString = "My Bug";

$myString{0} = "m";
$myString(5] = "s";

echo $myString;
?>

The result of the above script will change the string from:

My Bug

to:

my Bus

Creating PHP heredoc Strings

The PHP heredoc string syntax allows free form text to be used without having to worry about escaping special characters such as quotes and backslashes. The content of the heredoc string is wrapped with <<<EOD and EOD; markers.

The only rules are that the closing EOD; must be at the beginning of the last line, and the only content on that line, as follows:

<?php

$myString = <<<EOD
This is some free form text. It can span mutliple
lines and can contain otherwise troublesome characters like
\ and " and ' without causing any problems.
EOD;

echo $myString;

?>

Purchase and download the full PDF and ePub versions of this PHP eBook for only $8.99