13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Examining Variable Types<br />

29<br />

Examining Variable Types<br />

A variable’s type refers to the kind of data stored in it. <strong>PHP</strong> provides a set of data types.<br />

Different data can be stored in different data types.<br />

<strong>PHP</strong>’s Data Types<br />

<strong>PHP</strong> supports the following basic data types:<br />

n Integer—Used for whole numbers<br />

n<br />

n<br />

n<br />

n<br />

Float (also called double)—Used for real numbers<br />

String—Used for strings of characters<br />

Boolean—Used for true or false values<br />

Array—Used to store multiple data items (see Chapter 3,“Using Arrays”)<br />

n Object—Used for storing instances of classes (see Chapter 6)<br />

Two special types are also available: NULL <strong>and</strong> resource.Variables that have not been<br />

given a value, have been unset, or have been given the specific value NULL are of type<br />

NULL. Certain built-in functions (such as database functions) return variables that have<br />

the type resource. They represent external resources (such as database connections).You<br />

will almost certainly not directly manipulate a resource variable, but frequently they are<br />

returned by functions <strong>and</strong> must be passed as parameters to other functions.<br />

Type Strength<br />

<strong>PHP</strong> is called weakly typed, or dynamically typed language. In most programming languages,<br />

variables can hold only one type of data, <strong>and</strong> that type must be declared before<br />

the variable can be used, as in C. In <strong>PHP</strong>, the type of a variable is determined by the<br />

value assigned to it.<br />

For example, when you created $totalqty <strong>and</strong> $totalamount, their initial types were<br />

determined as follows:<br />

$totalqty = 0;<br />

$totalamount = 0.00;<br />

Because you assigned 0, an integer, to $totalqty, this is now an integer type variable.<br />

Similarly, $totalamount is now of type float.<br />

Strangely enough, you could now add a line to your script as follows:<br />

$totalamount = ‘Hello’;<br />

The variable $totalamount would then be of type string. <strong>PHP</strong> changes the variable type<br />

according to what is stored in it at any given time.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!