[ Pobierz całość w formacie PDF ]
Foundation PHP 5
for Flash
David Powers
Foundation PHP 5 for Flash
Copyright © 2005 by David Powers
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the
prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-466-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of
a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner,
with no intention of infringement of the trademark.
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY
10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, e-mail orders@springer-ny.com, or visit www.springer-ny.com.
Outside the United States: fax +49 6221 345229, e-mail orders@springer.de, or visit http://www.springer.de.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710.
Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the
preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or
damage caused or alleged to be caused directly or indirectly by the information contained in this work.
The source code for this book is freely available to readers at www.friendsofed.com in the Downloads section.
Credits
Lead Editors
Steve Rycroft
Chris Mills
Production Manager
Kari Brooks-Copony
Production Editor
Katie Stence
Technical Reviewer
Sham Bhangal
Compositor
Dina Quan
Editorial Board
Steve Anglin, Dan Appleman,
Ewan Buckingham, Gary Cornell,
Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills,
Dominic Shakeshaft,
Jim Sumser
Proofreader
Elizabeth Berry
Indexer
Michael Brinkman
Associate Publisher
Grace Wong
Artist
Kinetic Publishing Services, LLC
Project Manager
Beckie Stones
Cover Designer
Kurt Krames
Copy Editor
Nicole LeClerc
Manufacturing Manager
Tom Debolski
CONTENTS
About the Author
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
About the Technical Reviewer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Chapter 1: Getting Ready to Work with PHP
. . . . . . . . . . . . . . . . . . . 1
Accessing external data with Flash
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Choosing the right technology
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What PHP, Apache, and MySQL have to offer
. . . . . . . . . . . . . . . . . . . . . . . 4
How everything fits together
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
PHP and ActionScript: Distant cousins
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Installing the necessary software
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Setting up on Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Setting up on Mac OS X
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Setting up your work environment
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Getting a first taste of the power of PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Chapter 2: Flash Breaks Free
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Communicating with external sources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Taking first steps in PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
How PHP fits into web design
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
The basic grammar of PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Sending feedback from Flash by email
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Progress so far
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Chapter 3: Calculations and Decisions
. . . . . . . . . . . . . . . . . . . . . . . . 85
Performing calculations with PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Working with arithmetic operators
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Useful math functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Performing calculations in the right order
. . . . . . . . . . . . . . . . . . . . . . . . 93
Combining calculations and assignment
. . . . . . . . . . . . . . . . . . . . . . . . . 95
Making decisions with PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Using if… else conditional statements
. . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Using comparison operators
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
iii
CONTENTS
Testing more than one condition
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using switch for long decision chains
. . . . . . . . . . . . . . . . . . . . . . . . . . 102
Using the conditional operator
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Flash application: A multiconverter
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Planning the conversion script
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Building the Flash interface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Summing up
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Chapter 4: Of Strings and Things
. . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Manipulating strings with PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
How PHP outputs strings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Changing case
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Working with substrings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Modularizing code with functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Understanding where PHP functions run
. . . . . . . . . . . . . . . . . . . . . . . . 145
Why roll your own?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Understanding how PHP and ActionScript functions handle variables
. . . . . . 147
Returning a value from a function
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Deciding where to put functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Completing the multiconverter script
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Formatting the main measurement units
. . . . . . . . . . . . . . . . . . . . . . . . 152
Handling gallons, pints, and liters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Dealing with kilograms, pounds, and stones
. . . . . . . . . . . . . . . . . . . . . . 160
Handling meters to feet and yards
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Reviewing the multiconverter project
. . . . . . . . . . . . . . . . . . . . . . . . . . 162
Taking the project further
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Dealing with user input
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Trimming leading and trailing whitespace
. . . . . . . . . . . . . . . . . . . . . . . 164
Stripping HTML tags
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Removing backslashes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Using regular expressions to identify patterns
. . . . . . . . . . . . . . . . . . . . . 166
Fine-tuning the feedback application
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
A pause for breath
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Chapter 5: Working Smarter with Arrays and Loops
. . . . . . . . . . . . 177
Understanding the basics of arrays and loops
. . . . . . . . . . . . . . . . . . . . . . . . . 178
Organizing items in arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Grouping similar items in multidimensional arrays
. . . . . . . . . . . . . . . . . . 179
Using loops for repetitive tasks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Creating arrays in PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Indexed arrays: Organizing by number
. . . . . . . . . . . . . . . . . . . . . . . . . 181
Associative arrays: Organizing by name
. . . . . . . . . . . . . . . . . . . . . . . . . 184
Array length: Key to understanding the difference between
PHP and ActionScript arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Multidimensional arrays: Nesting arrays
. . . . . . . . . . . . . . . . . . . . . . . . . 189
Zipping through repetitive tasks with loops
. . . . . . . . . . . . . . . . . . . . . . . . . . 191
Looping through arrays with foreach
. . . . . . . . . . . . . . . . . . . . . . . . . . 191
iv
[ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • asael.keep.pl