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.

• The empty string ("") and the string "0"<br />

• An array with zero elements<br />

• An object with no values or functions<br />

• The NULL value<br />

Any value that is not false is true, including all resource values (which are described<br />

later, in the “Resources” section).<br />

<strong>PHP</strong> provides true and false keywords for clarity:<br />

$x = 5; // $x has a true value<br />

$x = true; // clearer way to write it<br />

$y = ""; // $y has a false value<br />

$y = false; // clearer way to write it<br />

Use the is_bool( ) function to test whether a value is a boolean:<br />

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

// $x is a boolean<br />

}<br />

Arrays<br />

An array holds a group of values, which you can identify by position (a number, with<br />

zero being the first position) or some identifying name (a string):<br />

$person[0] = "Edison";<br />

$person[1] = "Wankel";<br />

$person[2] = "Crapper";<br />

$creator['Light bulb'] = "Edison";<br />

$creator['Rotary Engine'] = "Wankel";<br />

$creator['Toilet'] = "Crapper";<br />

The array( ) construct creates an array:<br />

$person = array('Edison', 'Wankel', 'Crapper');<br />

$creator = array('Light bulb' => 'Edison',<br />

'Rotary Engine' => 'Wankel',<br />

'Toilet' => 'Crapper');<br />

There are several ways to loop across arrays, but the most common is a foreach loop:<br />

foreach ($person as $name) {<br />

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

}<br />

foreach ($creator as $invention => $inventor) {<br />

echo "$inventor created the $invention\n";<br />

}<br />

Hello, Edison<br />

Hello, Wankel<br />

Hello, Crapper<br />

Edison created the Light bulb<br />

Wankel created the Rotary Engine<br />

Crapper created the Toilet<br />

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

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

Data Types | 27

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

Saved successfully!

Ooh no, something went wrong!