25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12 ” <strong>PHP</strong> Basics<br />

ply the names of the data type you want to convert to enclosed in parentheses and<br />

placed before an expression. For example:<br />

$x = 10.88;<br />

echo (int) $x; // Outputs 10<br />

Note that a value cannot be converted to some special types; for example, you cannot<br />

convert any value to a resource—you can, however, convert a resource to a numeric<br />

or string data type, in which case <strong>PHP</strong> will return the numeric ID of the resource, or<br />

the string Resource id # followed by the resource ID.<br />

Variables<br />

Variables are temporary storage containers. In <strong>PHP</strong>, a variable can contain any type<br />

of data, such as, for example, strings, integers, floating-point numbers, objects and<br />

arrays. <strong>PHP</strong> is loosely typed, meaning that it will implicitly change the type of a variable<br />

as needed, depending on the operation being performed on its value. This contrasts<br />

with strongly typed languages, like C and Java, where variables can only contain<br />

one type of data throughout their existence.<br />

<strong>PHP</strong> variables are identified by a dollar sign $, followed by an identifier name. Variables<br />

must be named using only letters (a-z, A-Z), numbers and the underscore character;<br />

their names must start with either a letter or an underscore, and are one of only<br />

two identifier types in <strong>PHP</strong> that are case-sensitive (the other is constants, discussed<br />

below). Here are a few examples:<br />

$name = ’valid’; // Valid name<br />

$_name = ’valid’; // Valid name<br />

$1name = ’invalid’; // Invalid name, starts with a number<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

i<br />

Variables can also be interpolated—that is, inserted—directly into certain types of<br />

strings. This is described in the Strings chapter.

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

Saved successfully!

Ooh no, something went wrong!