QUE PHP by Example (2002).pdf

(2587 KB) Pobierz
Que - PHP By Examply.pdf
161245621.001.png
PHP
201 West 103rd Street
Indianapolis, Indiana 46290
To b y Butzon
BY EXAMPLE
161245621.002.png
PHP By Example
Associate Publisher
Dean Miller
Senior Acquisitions Editor
Jenny L. Watson
Development Editor
Sean Dixon
Technical Editor
Robert Grieger
Managing Editor
Thomas F. Hayes
Project Editor
Karen S. Shields
Indexer
Chris Barrick
Proofreaders
Bob LaRoche
Jeannie Smith
Team Coordinator
Cindy Teeters
Interior Designer
Karen Ruggles
Cover Designer
Rader Design
Copyright© 2002 by Que
All rights reserved. No part of this book shall be reproduced,
stored in a retrieval system, or transmitted by any means, elec-
tronic, mechanical, photocopying, recording, or otherwise, with-
out written permission from the publisher. No patent liability is
assumed with respect to the use of the information contained
herein. Although every precaution has been taken in the prepa-
ration of this book, the publisher and author assume no respon-
sibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained
herein.
International Standard Book Number: 0-7897-2568-1
Library of Congress Catalog Card Number: 2001090370
Printed in the United States of America
First Printing: November 2001
04 03 02 01
Trademarks
All terms mentioned in this book that are known to be trade-
marks or service marks have been appropriately capitalized. Que
cannot attest to the accuracy of this information. Use of a term
in this book should not be regarded as affecting the validity of
any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and
as accurate as possible, but no warranty or fitness is implied.
The information provided is on an “as is” basis. The author and
the publisher shall have neither liability nor responsibility to
any person or entity with respect to any loss or damages arising
from the information contained in this book.
4321
 
iii
Contents at a Glance
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Part I Getting Started with Programming in PHP 5
1Welcome to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
2 ariables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
3 Program Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
4 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
5 String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Part II Control Structures 113
6 The if , elseif , and else Statements . . . . . . . . . . . . . . . . . . . . . .114
7 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136
8Using while and do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
9Using for and foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Part III Organization and Optimization of Your Program 185
10 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186
11 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214
12 Using Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
Part IV Advanced PHP Features
261
13 Creating Dynamic Content with PHP and a
MySQL Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
14 Using PHP for Password Protection . . . . . . . . . . . . . . . . . . . . . . .292
15 Allowing Visitors to Upload Files . . . . . . . . . . . . . . . . . . . . . . . . .314
16 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
17 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352
APPENDIX 373
A Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . .374
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394
 
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Part I Getting Started with Programming in HP 5
1Welcome to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
Why PHP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
If You’re New to Programming… . . . . . . . . . . . . . . . . . . . . . . . . . .10
Writing a Basic PHP Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Programming Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Good Style: Using Whitespace and Comments . . . . . . . . . . .17
How Embedded Programming Works . . . . . . . . . . . . . . . . . .20
Server-Side Versus Client-Side Scripting . . . . . . . . . . . . . . . . . . . .22
Running Your New Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
What If It Didn’t Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
2 ariables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Introduction to Variables and Constants . . . . . . . . . . . . . . . . . . . .30
Declaration and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Assigning Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Declaring a Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Deciding Whether to Use a Variable or Constant . . . . . . . . .35
Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Necessity of Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Variable References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
3 Program Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Revisiting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
The echo Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Using Here-doc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Zgłoś jeśli naruszono regulamin