05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

<strong>PHP</strong> recognizes floating-point numbers written in two different formats. There’s the<br />

one we all use every day:<br />

3.14<br />

0.017<br />

-7.1<br />

but <strong>PHP</strong> also recognizes numbers in scientific notation:<br />

0.314E1 // 0.314*10 1, or 3.14<br />

17.0E-3 // 17.0*10 -3, or 0.017<br />

Floating-point values are only approximate representations of numbers. For example,<br />

on many systems 3.5 is actually represented as 3.4999999999. This means you<br />

must take care to avoid writing code that assumes floating-point numbers are represented<br />

completely accurately, such as directly comparing two floating-point values<br />

using ==. The normal approach is to compare to several decimal places:<br />

if (int($a * 1000) == int($b * 1000)) {<br />

// numbers equal to three decimal places<br />

Use the is_float( ) function (or its is_real( ) alias) to test whether a value is a floating<br />

point number:<br />

if (is_float($x)) {<br />

// $x is a floating-point number<br />

}<br />

Strings<br />

Because strings are so common in web applications, <strong>PHP</strong> includes core-level support<br />

for creating and manipulating strings. A string is a sequence of characters of arbitrary<br />

length. String literals are delimited by either single or double quotes:<br />

'big dog'<br />

"fat hog"<br />

Variables are expanded within double quotes, while within single quotes they are<br />

not:<br />

$name = "Guido";<br />

echo "Hi, $name\n";<br />

echo 'Hi, $name';<br />

Hi, Guido<br />

Hi, $name<br />

Double quotes also support a variety of string escapes, as listed in Table 2-2.<br />

Table 2-2. Escape sequences in double-quoted strings<br />

Escape sequence Character represented<br />

\" Double quotes<br />

\n Newline<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Data Types | 25

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

Saved successfully!

Ooh no, something went wrong!