11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

www.it-ebooks.infoCHAPTER 3 • <strong>PHP</strong> BASICS■ Note Alternative enclosure syntax is available for the if, while, for, foreach, and switch control structures.This involves replacing the opening bracket with a colon (:) and replacing the closing bracket with endif;,endwhile;, endfor;, endforeach;, and endswitch;, respectively. There has been discussion regardingdeprecating this syntax in a future release, although it is likely to remain valid for the foreseeable future.The else StatementThe problem with the previous example is that output is only offered for the user who correctly guessesthe secret number. All other users are left destitute, completely snubbed for reasons presumably linkedto their lack of psychic power. What if you want to provide a tailored response no matter the outcome?To do so you would need a way to handle those not meeting the if conditional requirements, a functionhandily offered by way of the else statement. Here’s a revision of the previous example, this timeoffering a response in both cases:Like if, the else statement brackets can be skipped if only a single code statement is enclosed.The elseif StatementThe if-else combination works nicely in an “either-or” situation—that is, a situation in which only twopossible outcomes are available. But what if several outcomes are possible? You would need a means forconsidering each possible outcome, which is accomplished with the elseif statement. Let’s revise thesecret-number example again, this time offering a message if the user’s guess is relatively close (withinten) of the secret number:

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

Saved successfully!

Ooh no, something went wrong!