Understanding PHP Variable Types

From Techotopia
Revision as of 19:40, 25 May 2007 by Neil (Talk | contribs) (The PHP String Variable)

Jump to: navigation, search

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


Contents


The PHP Integer Variable Type

Integer variables are able to hold a whole number in the range of -2147483648 to 2147483647. Negaitve values can be asssigned by palcing the minus (-) sign after the assigment 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;

?>

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 intialization 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 Looping and Flow Control, with particular regard to the if statement. It is useful to know that the true and false values are actually represented internally by PHP as the numbers 1 and 0 respectively.

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 surrounding it in single qoutes (') or double qoutes ("). If your string itself contains either single or double qoutes 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 preceeding 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 sepcial 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 varipous control sequences and their respective descriptions:

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

Converting Between PHP Strings, Numbers and Floats