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 alternate syntax is:<br />

foreach ($array as $key => $value):<br />

// ...<br />

endforeach;<br />

declare<br />

The declare statement allows you to specify execution directives for a block of code.<br />

The structure of a declare statement is:<br />

declare (directive)<br />

statement<br />

Currently, there is only one declare form, the ticks directive. Using it, you can specify<br />

how frequently (measured roughly in number of code statements) a tick function<br />

registered with register_tick_function( ) is called. For example:<br />

register_tick_function("some_function");<br />

declare(ticks = 3) {<br />

for($i = 0; $i < 10; $i++) {<br />

// do something<br />

}<br />

}<br />

In this code, some_function( ) is called after every third statement is executed.<br />

exit and return<br />

The exit statement ends execution of the script as soon as it is reached. The return<br />

statement returns from a function or (at the top level of the program) from the script.<br />

The exit statement takes an optional value. If this is a number, it’s the exit status of<br />

the process. If it’s a string, the value is printed before the process terminates. The<br />

exit( ) construct is an alias for die( ):<br />

$handle = @mysql_connect("localhost", $USERNAME, $PASSWORD);<br />

if (!$handle) {<br />

die("Could not connect to database");<br />

}<br />

This is more commonly written as:<br />

$handle = @mysql_connect("localhost", $USERNAME, $PASSWORD)<br />

or die("Could not connect to database");<br />

See Chapter 3 for more information on using the return statement in functions.<br />

Including Code<br />

<strong>PHP</strong> provides two constructs to load code and HTML from another module: require<br />

and include. They both load a file as the <strong>PHP</strong> script runs, work in conditionals and<br />

54 | Chapter 2: Language Basics<br />

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

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!