Sunday, October 5, 2008

Teach Yourself PHP 4 in 24 hours


No tf or co m er cia l, us e fo re du ca http://i-tronics.blogspot.com/ tio n 1 on ly 3 originally graduated in philosophy and has learned his trade by reinventing wheels and then working out why they don't run straight. Matt has taught courses in HTML, JavaScript, Perl, and PHP and was a contributing author to Dynamic HTML Unleashed. When not coding, Matt is a committed urban cyclist, a Guinness drinker, an obsessive reader, and a writer of unpublishable short stories. One day he claims he will write a novel. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 4 Acknowledgments The open source concept made both my career and this book possible. I would like to thank all those people whose voluntary efforts continue to defy the received wisdom. Particular thanks to the PHP community, especially contributors to the PHP mailing lists whose postings revealed pitfalls, suggested techniques, and kept me amused. From Macmillan, I would like to thank Randi Roger for suggesting me for this project as well as Jeff Schultz, Paul Schneider, and Scott Meyers for support and tolerance as deadlines loomed and panic set in. extreme vagueness on any matter not pertaining to PHP. In particular, my business partner circumstances, and Dave Urmson, who took over formatting when the going got tough. Other Corrosive stars include Anisa Swaffield, Jeff Coburn, Mai Chokelumlerd, and Moira Govern. I must also thank Small Planet (http://www.smallpla.net) for providing me with additional development space and allowing me to use it to play with beta software. Particular thanks to Mohammed Abba and Clive Hills, who recompiled PHP on the Small Planet system more times than we care to remember. graciously agreed to act as guinea pigs. One of the best ways to test a tutorial text is to use it in class. Thanks to my PHP students who Thanks also to my partner Louise and our new daughter Holly for being there, and bearing the As my social life took second place to PHP, my local became a refuge for last minute pint and proofing sessions. Thanks to Alan and Dora of the Prince Arthur for running the perfect pub. Tell Us What You Think! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can fax, email, or write me directly to let me know what you did or didn't like about this book— as well as what we can do to make our books stronger. No Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book's title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book. Fax: 317-581-4770 tf or co m er Finally, thanks to the fishes, who cheered up a lurker. cia l, grumpy, hunched, and obsessed character I became while writing this book. http://i-tronics.blogspot.com/ us e fo re du ca Massimo Guglielmino, who kept the Corrosive show on the road under the usual stressful tio n Thanks must also go to all at Corrosive for putting up with my continued absence and my on ly 5 Email: Mail: Mark Taber Associate Publisher Sams Publishing 201 West 103rd Street Indianapolis, IN 46290 USA No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 6 Introduction This is a book about PHP, the open source Web scripting language that has joined Perl, ASP, and Java on the select list of languages that can be used to create dynamic online environments. It is also a book about programming. In the space available, it is neither technique that PHP offers. Nevertheless, whether you are an experienced programmer enough information to get your journey off to a good start. possible to create a complete guide to programming in PHP nor to cover every function and considering a move to PHP or a newcomer to scripting, the steps in this book should provide Who Should Read This Book? This book will take you from the first principles through to a good working knowledge of the PHP4 programming language. No prior experience of programming is assumed, though if you have worked with a language such as C or Perl in the past, you will find the going much easier. PHP4 is a Web programming language. To get the most from this book, you should have some understanding of the World Wide Web and of HTML in particular. If you are just starting out, you are comfortable creating basic documents and can build a basic HTML table, you will be fine. you will still be able to use this book, though you should consider acquiring an HTML tutorial. If written to work with MySQL, a SQL database that is free for personal use on some platforms. We include a short introduction to SQL, but if you intend to use PHP to work with databases, you might want to spend some time reading up on the subject. Numerous introductory SQL tutorials are available online. If you intend to work with a database other than MySQL, many of functions designed to query your database. the examples in this book will be relatively easy to reproduce with the equivalent PHP No How This Book Is Organized This book is divided into four parts: Part 1 is an introduction to PHP4. Part 2 covers the basic features of the language. Pay particular attention to this section if you are new to programming. tf or co m er cia l, PHP4 is designed to integrate well with databases. Some of the examples in this book are http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 7 Part 3 covers PHP4 in more detail, looking at the functions and techniques you will need to become a proficient PHP programmer. Part 4 illustrates a complete self-contained example. Part 1 contains Hours 1 through 3 and handles the information you will need to get your first script up and running: and looks at some of the compelling reasons for deciding to learn this scripting language. Hour 1, "PHP: From Home Page to Portal," describes the history and capabilities of PHP Hour 2, "Installing PHP," explains how to install PHP on a UNIX system and discusses hour, we also look at PHP configuration options. Hour 3, "A First Script," looks at the different ways in which you can embed a PHP script in a document and create a script that writes text to the user's browser. Part 2 comprises Hours 4 through 8. In this part, you will learn the basic components of the PHP language: data types, operators, and expressions. Hour 4, "The Building Blocks," covers the basics of PHP. You will learn about variables, Hour 5, "Going with the Flow," covers the syntax for controlling program flow in your scripts. Hour 6, "Functions," explores the use of functions to organize your code. We will also look at some of the functions that PHP4 provides to manipulate arrays. Hour 8, "Objects," introduces PHP4's support for classes and objects. Throughout the course of the hour, we will develop a working example. Part 3 consists of Hours 9 through 22. In this part, you will come to grips with the features and techniques of the language: Hour 9, "Working with Forms," introduces the dimension of user input through the Hour 10, "Working with Files," shows you how to work with files and directories on the local machine. Hour 11, "Working with the DBM Functions," demonstrates PHP4's support for DBM Hour 12, "Database Integration— MySQL," provides a brief introduction to SQL syntax and introduces the PHP4 functions that can be used to work with the MySQL database. Hour 13, "Beyond the Box," covers some of the details of HTTP requests and looks at PHP network functions. No tf database systems, versions of which are available on most systems. or mechanism of the HTML form. You will learn how to gather data submitted via a form. co m er cia l, Hour 7, "Arrays," discusses the array data type that can be used to hold list information. http://i-tronics.blogspot.com/ us statements. e In addition to if and switch constructs, you will learn about loops using for and while fo re du ca tio n some of the configuration options you might want to choose when compiling PHP. In this on ly 8 Hour 14, "Working with Dynamic Images" explores PHP's image functions. With these, you can create GIF or PNG files dynamically. Hour 15, "Working with Dates," covers the functions and techniques you can use for date arithmetic. We create a calendar example. Hour 16, "Working with Data," revisits data types and explores some more of the functions you can use to work with data in your scripts. More array functions are also covered. Hour 17, "Working with Strings," covers the functions that you can use to manipulate strings. You can use these to find and replace complex patterns in strings. passing information across scripts and requests. 19, using PHP4's built-in session functions. Hour 18, "Working with Regular Expressions," introduces regular expression functions. Hour 19, "Saving State with Cookies and Query Strings," shows you some techniques for Hour 20, "Saving State with Session Functions," extends the techniques explored in Hour Hour 21, "Working with the Server Environment," shows you how to call external programs from your scripts and incorporate their output into your own. Hour 22, "Debugging," shows you some techniques that you can use to track down problems in your code. We also examine some common errors. some of the techniques that were introduced earlier in the book. Hour 23, "An Example (Part 1)," creates a brief for a club listings script. We build the code that will allow users to create accounts and enter listings. Hour 24, "An Example (Part 2)," concludes the project, building the code for nonmembers to browse the listings and look at club profiles. No tf or co m er cia l, http://i-tronics.blogspot.com/ us Part 4 consists of Hours 23 and 24. In these, we build a working example that incorporates e fo re du ca tio n on ly 9 co Overview tf 29 Platforms, Servers, Databases, and PHP Where to Find PHP and More 30 Installing PHP4 for Linux and Apache 30 32 --enable-track-vars 32 --with-gd 32 --with-mysql 33 m er Hour 1: PHP: From Home Page to Portal Overview 22 What Is PHP? 22 How Did PHP Evolve? 23 What's New in PHP4 24 The Zend Engine 24 Why Choose PHP? 25 Speed of Development 25 PHP Is Open Source 26 Performance 26 Portability 26 Summary 27 Q&A 27 Workshop 27 Quiz 27 Activity 28 Hour 2: Installing PHP 29 cia l, us e No or Some configure Options Configuring Apache 33 php.ini 34 short_open_tag 35 Error Reporting Directives 35 http://i-tronics.blogspot.com/ fo re du ca 29 tio 22 n SAMS Teach Yourself PHP4 in 24 Hours Acknowledgments 4 Tell Us What You Think! 4 Introduction 6 Who Should Read This Book? 6 How This Book Is Organized 6 2 on ly 10 m er Hour 4: The Building Blocks49 Overview 49 Variables 49 Dynamic Variables 50 References to Variables 52 Data Types 53 Changing Type with settype() Changing Type by Casting 57 cia l, us e Hour 3: A First Script 40 Overview 40 Our First Script 40 Beginning and Ending a Block of PHP Statements The print() Function 44 Combining HTML and PHP 44 Adding Comments to PHP Code 46 Summary 47 Q&A 47 Workshop 47 Quiz 48 Activity 48 or Operators and Expressions 58 The Assignment Operator Arithmetic Operators 59 co No tf The Concatenation Operator More Assignment Operators60 Comparison Operators 61 Creating More Complex Test Expressions with the Logical Operators 62 Automatically Incrementing and Decrementing an Integer Variable 63 Operator Precedence 65 http://i-tronics.blogspot.com/ fo re du ca 55 59 60 tio n 42 on ly Variable Directives 36 Help! 36 Summary 38 Q&A 38 Workshop 38 Quiz 38 Activity 39 11 What Is a Function? Calling Functions 88 Defining a Function 90 m er Constants 66 Predefined Constants 67 Summary 67 Q&A 67 Workshop 68 Quiz 68 Activities 69 Hour 5: Going with the Flow 70 Overview 70 Switching Flow 70 The if Statement 70 Using the else Clause with the if Statement 72 Using the elseif Clause with the if Statement 73 The switch Statement 75 Using the ? Operator 77 Loops 78 The while Statement 78 The do..while Statement 79 The for Statement 80 Breaking Out of Loops with the break Statement 82 Skipping an Iteration with the continue Statement 84 Nesting Loops 85 Summary 86 Q&A 86 Hour 6: Functions 88 Overview 88 No tf or Returning Values from User-Defined Functions 92 Dynamic Function Calls 93 Variable Scope 94 Accessing Variables with the global Statement 95 Saving State Between Function Calls with the static Statement 98 More About Arguments 100 Setting Default Values for Arguments 100 Passing References to Variables to Functions co cia l, us 88 e http://i-tronics.blogspot.com/ fo re du ca tio 102 n on ly 12 Summary 105 Q&A 105 Workshop 105 Quiz 106 Activity 107 Hour 7: Arrays 108 Overview 108 What Is an Array? 108 Creating Arrays 109 Defining Arrays with the array() Function 109 Defining or Adding to Arrays with the Array Identifier 110 Associative Arrays 110 Defining Associative Arrays with the array() Function 111 Directly Defining or Adding to an Associative Array 111 Multidimensional Arrays 112 Accessing Arrays 113 Getting the Size of an Array 113 Looping Through an Array 114 Looping Through an Associative Array 115 Outputting a Multidimensional Array 116 Manipulating Arrays118 Joining Two Arrays with array_merge() 119 Adding Multiple Variables to an Array with array_push() 119 No Sorting Numerically Indexed Arrays with sort() 122 Sorting an Associative Array by Value with asort() 123 Sorting an Associative Array by Key with ksort() 123 Summary 124 124 Workshop 125 Quiz 125 Activities 125 Hour 8: Objects126 Overview 126 tf or Q&A co Slicing Arrays with array_slice() Sorting Arrays 122 m er Removing the First Element of an Array with array_shift() 120 121 cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 13 Quiz 152 Activities 153 Hour 9: Working with Forms co m er What Is an Object? 126 Creating an Object 127 Object Properties 128 Object Methods 129 An Example132 Defining the Class's Properties 133 Creating a Constructor 133 The addRow() Method 134 The addRowAssocArray() Method 134 The output() Method 135 Bringing It All Together 136 What's Missing?139 Why a Class? 139 Inheritance 140 Overriding the Method of a Parent Class 141 Calling an Overridden Method 143 Inheritance: An Example 144 Defining HTMLTable's Properties144 Creating the Constructor 145 The setCellpadding() Method 146 The Output() Method 146 The Table and HTMLTable Classes in Their Entirety 147 Why Use Inheritance? 150 Summary 151 Q&A 152 Workshop 152 No tf or Overview 154 Global and Environment Variables 154 A Script to Acquire User Input 156 Accessing Input from Multiple SELECT Elements cia l, us e 154 Accessing All the Fields from a Form in an Associative Array 159 Distinguishing Between GET and POST Transactions 161 Combining HTML and PHP Code on a Single Page 162 Using Hidden Fields to Save State 165 http://i-tronics.blogspot.com/ fo re du ca tio 157 n on ly 14 Hour 10: Working with Files Overview 175 175 Working with Directories No Quiz 194 Activities 195 Hour 11: Working with the DBM Functions 196 tf or Reading the Contents of a Directory with readdir() 192 Summary 194 Q&A 194 Workshop 194 co 191 Creating Directories with mkdir() 191 Removing a Directory with rmdir() 192 Opening a Directory for Reading with opendir() 192 m er Reading Characters from a File with fgetc() 188 Writing or Appending to a File 189 Writing to a File with fwrite() or fputs() 189 Locking Files with flock() 190 cia l, Reading Arbitrary Amounts of Data from a File with fread() 186 http://i-tronics.blogspot.com/ us Including Files with include() 175 Testing Files 178 Checking for Existence with file_exists() 179 A File or a Directory? 179 Checking the Status of a File 179 Determining File Size with filesize() 180 Getting Date Information About a File 180 Creating a Function That Performs Multiple File Tests 181 Creating and Deleting Files 183 Opening a File for Writing, Reading, or Appending 183 Reading from Files 184 Reading Lines from a File with fgets() and feof() 185 e fo re du ca tio n on ly Redirecting the User 167 File Upload Forms and Scripts 169 Summary 173 Q&A 173 Workshop 173 Quiz 174 Activities 174 15 227 Listing the Tables Within a Database 229 Listing and Exploring Fields 229 Database Structure— Bringing It All Together 230 Summary 232 Q&A 233 Workshop 233 Quiz 234 235 No Activities 234 Hour 13: Beyond the Box Overview 235 tf or co Listing Databases m er Hour 12: Database Integration— MySQL213 Overview 213 A (Very) Brief Introduction to SQL 213 Connecting to the Database Server 214 Selecting a Database 215 Finding Out About Errors 215 Adding Data to a Table 216 Acquiring the Value of an Automatically Incremented Field 220 Accessing Information 221 Finding the Number of Rows Found by a Query 221 Accessing a Resultset 222 Changing Data 225 Getting Information About Databases 227 cia l, http://i-tronics.blogspot.com/ us e fo re du ca Overview 196 Opening a DBM Database 196 Adding Data to the Database 197 Amending Elements in a Database 198 Reading from a DBM Database 199 Determining Whether an Item Exists in a Database 201 Deleting an Item from a Database 201 Adding Complex Data Structures to a DBM Database 201 An Example205 Summary 211 Q&A 211 Workshop 211 Quiz 211 Activities 212 tio n on ly 16 m er Sending Mail with the mail() Function 252 Summary 253 Q&A 253 Workshop 254 Quiz 254 Activities 254 Hour 14: Working with Dynamic Images 256 Overview 256 Creating and Outputting Images 256 Acquiring Color 257 Drawing Lines 258 Applying Color Fills 259 Drawing an Arc 260 Drawing a Rectangle 261 Drawing a Polygon 262 Making a Color Transparent 263 Working with Text 264 Writing a String with imageTTFtext() 265 Testing Text Dimensions with imageTTFbox() cia l, us e Bringing It Together co 270 Summary 275 Q&A 275 Workshop 276 Quiz 276 Activities 276 Hour 15: Working with Dates Overview 277 No tf or 277 Getting the Date with time() 277 Converting a Time Stamp with getdate() 278 Converting a Time Stamp with date() 279 http://i-tronics.blogspot.com/ fo re du ca 266 tio n on ly Environmental Variables 235 A Brief Summary of an HTTP Client/Server Negotiation 238 The Request 238 The Response 241 Getting a Document from a Remote Address 243 Converting IP Addresses and Hostnames 244 Making a Network Connection 245 Making an NNTP Connection Using fsockopen() 249 17 Hour 16: Working with Data 294 Overview 294 Data Types Revisited 294 A Recap 294 Converting Complex Types 295 Automatic Conversion of Data Types 297 Testing Data Types 298 More Ways of Changing Type 300 Why Are Data Types Important? 300 Testing for Absence and Emptiness 302 More About Arrays 303 An Alternative Approach to Traversing Arrays 304 Checking That a Value Exists in an Array 305 Removing an Element from an Array 306 Applying a Function to Every Element in an Array 306 Custom Sorting Arrays 307 Summary 311 Q&A 311 311 Quiz 311 Activities 312 Hour 17: Working with Strings 313 Overview 313 Formatting Strings 313 No tf or Workshop co Working with printf() 313 printf() and Type Specifiers 314 Padding Output with the Padding Specifier 317 m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio Creating Time Stamps with mktime() Testing a Date with checkdate() An Example283 Checking User Input 284 Building the HTML Form 285 Creating the Calendar Table287 Summary 292 Q&A 292 Workshop 292 Quiz 292 Activity 293 282 283 n on ly 18 m er Hour 18: Working with Regular Expressions 334 Overview 334 POSIX Regular Expression Functions 334 Using ereg() to Match Patterns in Strings cia l, Specifying a Field Width 318 Specifying Precision 319 Conversion Specifications: A Recap 320 Storing a Formatted String 322 Investigating Strings 323 A Note About Indexing Strings 323 Finding the Length of a String with strlen() 324 Finding a Substring Within a String with strstr() 324 Finding the Position of a Substring with strpos() 325 Extracting Part of a String with substr() 325 Tokenizing a String with strtok() 326 Manipulating Strings 328 Cleaning Up a String with trim() and ltrim()328 Replacing a Portion of a String using substr_replace() 329 Replacing Substrings Using str_replace 329 Converting Case 329 Breaking Strings into Arrays with explode() 331 Summary 331 Q&A 332 Workshop 332 Quiz 332 Activities 333 us e No tf or co Using Quantifiers to Match a Character More Than Once 335 Matching Ranges of Characters with Character Classes 338 Working with Atoms 339 Branches 340 Anchoring a Regular Expression 340 342 The Membership Code Example Revisited 341 Using egrep_replace() to Replace Patterns in Strings Using Back References with egrep_replace() 342 Using split() to Break Up Strings 343 Perl Compatible Regular Expressions (PCREs) 343 Matching Patterns with preg_match() 343 http://i-tronics.blogspot.com/ fo re du ca 335 tio n on ly 19 Hour 19: Saving State with Cookies and Query Strings 356 Overview 356 Cookies 356 The Anatomy of a Cookie 357 Setting a Cookie with PHP 358 Deleting a Cookie 360 Creating Session Cookies 360 An Example— Tracking Site Usage 361 Working with the Query String 369 Creating a Query String370 Summary 372 Q&A 373 Workshop 373 Quiz 373 Activities 374 Hour 20: Saving State with Session Functions 375 Overview 375 What Are Session Functions? 375 Starting a Session with session_start() 376 No tf or Working with Session Variables 377 Destroying Sessions and Unsetting Variables 382 Passing Session IDs in the Query String 383 384 Checking that a Session Variable Is Registered 385 Summary 385 Q&A 386 Workshop Quiz 386 386 Encoding and Decoding Session Variables co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n PCREs and Greediness 344 PCREs and Backslashed Characters 345 Finding Matches Globally with preg_match_all() Using preg_replace() to Replace Patterns 350 Modifiers 351 Summary 354 Q&A 354 Workshop 355 Quiz 355 Activity 355 347 on ly 20 Activities 386 Hour 21: Working with the Server Environment 388 Overview 388 Opening Pipes to and from Processes with popen() 388 Running Commands with exec() 392 Running External Commands with system() or the Backtick Operator 393 Plugging Security Holes with escapeshellcmd() 394 Running External Applications with passthru() 396 Calling an External CGI Script with the virtual() Function 397 Summary 398 Q&A 398 Workshop 399 Quiz 399 Activities 400 Hour 22: Debugging 401 Overview 401 Getting Information About PHP and Your Script 401 phpinfo() 401 Viewing Source with Syntax Coloring 405 PHP Error Messages 407 Writing Error Messages to a Log File 410 Getting the Error String 412 Manual Debugging 412 Common Errors 414 Summary 416 Q&A 417 Workshop 417 417 Activity 417 Hour 23: An Example (Part 1) 418 Overview 418 The Brief 418 The Structure 418 Designing the Database419 No tf or Design Choices 421 The Members Environment 421 join.php and dblib.inc 421 co Quiz m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 21 No tf or co m er cia l, Hour 24: An Example (Part 2) 457 Overview 457 The Events Diary Public Screens viewevents.php 457 viewclubs.php 466 viewclub.php 470 viewevent.php 473 The Future 476 Summary 477 Q&A 477 Workshop 477 Quiz 477 Activities 478 http://i-tronics.blogspot.com/ us e fo re du ca 457 tio n updateclub.php 429 membersmenu.php 437 login.php 438 updateevent.php 441 reviewevents.php 450 Summary 455 Q&A 455 Workshop 456 Quiz 456 Activity 456 on ly 22 Hour 1: PHP: From Home Page to Portal Welcome to PHP! Throughout this book you will look at almost every element of the future. In this hour, you will learn What PHP is About PHP's history PHP language. But first you will explore PHP as a product— its history, features, and What improvements can be found in PHP4 Some options that add features to your PHP binary Some reasons you should choose to work with PHP PHP is a language that has outgrown its name. It was originally conceived as a set of macros to help coders maintain personal home pages, and its name grew from its purpose. Since then, PHP's capabilities have been extended, taking it beyond a set database-driven online environments. As PHP's capabilities have grown, so too has its popularity. According to NetCraft (http://www.netcraft.com), PHP was running on more than 1 million hosts in November 1999. As of February 2000, that figure had already risen to 1.4 million hosts. According to E-Soft, PHP is the most popular Apache module available, beating even ModPerl. of utilities to a full-featured programming language, capable of managing huge No PHP is now officially known as PHP: HyperText Preprocessor. It is a server-side scripting language usually written in an HTML context. Unlike an ordinary HTML page, a PHP script is not sent directly to a client by the server; instead, it is parsed by the PHP binary or module. HTML elements in the script are left alone, but PHP code is interpreted and executed. PHP code in a script can query databases, create images, read and write files, talk to remote servers— the possibilities are endless. tf or co m er cia l, http://i-tronics.blogspot.com/ us What Is PHP? e fo re du ca tio n on ly Overview 23 The output from PHP code is combined with the HTML in the script and the result sent to the user. How Did PHP Evolve? publishing macros. These were released as the Personal Home Page Tools and later From a user's perspective, PHP/FI was already an attractive proposition, and its community. By 1997, a team of programmers was working on the project. The next release— PHP3— was born out of this collaborative effort. PHP3 was an rewritten and extended to include a package called the Form Interpreter (PHP/FI). popularity grew steadily. It also began to attract interest from the developer effective rewrite of PHP, with an entirely new parser created by Zeev Suraski and Andi Gutmans, as well as differences in syntax and new features. This release established PHP as one of the most exciting server scripting languages available, and the growth in usage was enormous. module. MySQL is a powerful free SQL database, and PHP provides a comprehensive all but unbeatable. That isn't to say that PHP is not designed to work in other environments and with other tools. In fact, PHP supports a bewildering array of databases and servers. The rise in popularity of PHP has coincided with a change of approach in Web publishing. In the mid-1990s it was normal to build sites, even relatively large sites, with hundreds of individual hard-coded HTML pages. Increasingly, though, site publishers are harnessing the power of databases to manage their content more effectively and to personalize their sites according to individual user preferences. No The use of databases to store content, and of a scripting language to retrieve this data, will become further necessary as data is sent from a single source to multiple environments, including mobile phones and PDAs, digital television, and broadband Internet environments. In this context, it is not surprising that a tool of PHP's sophistication and flexibility is becoming so popular. tf or co m er cia l, set of functions for working with it. The combination of Apache, MySQL, and PHP is http://i-tronics.blogspot.com/ us the most-used Web server in the world, and PHP3 can be compiled as an Apache e PHP's support for Apache and MySQL further secured its popularity. Apache is now fo re du ca tio n on ly The first version of PHP was created by Rasmus Lerdorf in 1994 as a set of Web 24 At the time of this writing, PHP4 is in its final beta stage and is due for release shortly. By the time you read this book, PHP4 will be making waves! What's New in PHP4 interesting. Let's take a quick look at some of them. A new foreach statement, similar to that found in Perl, makes it much easier to book. Additionally, a raft of new array functions have been added, making arrays easier to manipulate. The language now includes the boolean data type. A particularly useful feature of PHP3 was the capability to name form elements as if they were elements in an array. The elements' names and values are then made available to the code in array form. This feature has been extended to support multidimensional arrays. Support for object-oriented programming was somewhat rudimentary in PHP. This is significantly extended in PHP4; for example, it is now possible to call an overridden method from a child class. PHP4 now provides native support for user sessions, using both cookies and the query string. You can now "register" a variable with a session, and then access the same variable name and value in subsequent user requests. A new comparison operator (===) has been introduced that tests for New associative arrays containing server and environmental variables have been made available, as well as a variable that holds information about uploaded PHP4 now provides built-in support for both Java and XML. Although these and other features significantly improve the language, perhaps the files. equivalence of type as well as equivalence of value. No The Zend Engine When PHP3 was written, an entirely new parser was created from the ground up. PHP4 represents a similar change to the scripting engine. This rewrite, though, is more significant by orders of magnitude. tf most significant change has taken place under the hood. or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n loop through arrays. We will be using this for most of the array examples in this on ly PHP4 introduces numerous new features that will make the programmer's life more 25 Zend is a scripting engine that sits below the PHP-specific modules. It is optimized to significantly improve performance. These changes in efficiency will ensure PHP4's continued success. Most code written for PHP3 will continue to run with no changes; however, these scripts may run up to 200 times faster! A commercial addition to the Zend engine will be the facility for compiling PHP all, competitors far behind. scripts. This will provide a further gain in performance that should leave most, if not Communication with servers has been improved, so it will be possible to create PHP modules that work with a wider range of servers. Unlike a CGI interpreter, which sits outside a server and is initialized every time a script is run, a server module runs in conjunction with the server. This improves performance because the scripting engine does not need to be started for a PHP page to be executed. Why Choose PHP? There are some compelling reasons to work with PHP4. For many projects you will find that the production process is significantly faster than you might expect if you are used to working with other scripting languages. As an open source product, PHP4 is well supported by a talented production team and a committed user community. Furthermore, PHP can be run on all the major operating systems with most servers. Because PHP allows you to separate HTML code from scripted elements, you will instances, you will be able to separate the coding stage of a project from the design and build stages. Not only can this make life easier for you as a programmer, it also can remove obstacles that stand in the way of effective and flexible design. No tf notice a significant decrease in development time on many projects. In many or co Speed of Development m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio Zend is built to improve performance but is also designed for increased flexibility. n on ly 26 PHP Is Open Source To many people, "open source" simply means free, which is, of course, a benefit in itself. To quote from the official PHP site at http://www.php.net/: background, but PHP doesn't cost anything. You can use it for commercial and/or non-commercial use all you want. You can give it to your friends, print it out and This may sound a little foreign to all you folks coming from a non-UNIX license. Well-maintained open source projects offer users additional benefits, though. You benefit from an accessible and committed community who offer a wealth of experience in the subject. Chances are that any problem you encounter in your coding can be answered swiftly and easily with a little research. If that fails, a question sent to a mailing list can yield an intelligent, authoritative response. You also can be sure that bugs will be addressed as they are found, and that new the next commercial release before taking advantage of improvements. There is no vested interest in a particular server product or operating system. You are free to make choices that suit y our needs or those of your clients, secure that your code will run whatever you decide. Performance Because of the powerful Zend engine, PHP4 compares well with ASP in benchmark tests, beating it in some tests. Compiled PHP leaves ASP far behind. Portability No PHP is designed to run on many operating systems and to cooperate with many servers and databases. You can build for a UNIX environment and shift your work to NT without a problem. You can test a project with Personal Web Server and install it on a UNIX system running on PHP as an Apache module. tf or co m er cia l, http://i-tronics.blogspot.com/ us features will be made available as the need is defined. You will not have to wait for e fo re du ca tio software! Smile, be happy, the world is good. For the full legalese, see the official n hang it on your wall or eat it for lunch. Welcome to the world of Open Source on ly 27 Summary In this hour, we introduced PHP. You learned the history of PHP from a simple set of macros to the powerful scripting environment it has become. You found out about PHP4 and the Zend scripting engine, and how they incorporate new features and more efficiency. Finally, you discovered some of the features that make PHP a compelling choice as a Web programming language. Q Is PHP an easy language to learn? A In short, yes! You really can learn the basics of PHP in 24 hours. PHP provides an enormous wealth of functions that allow you to do things for which you would have to write custom code in other languages. PHP also handles data types and memory issues for you (much like Perl). Understanding the syntax and structures of a programming language is only the beginning of the journey, however. Ultimately, you will only really learn by building your own projects and by making mistakes. You should see this book as a starting point. Workshop material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. Quiz No tf What did the initials PHP originally stand for? Who created the original version of PHP? What is the name of the new scripting engine that powers PHP? Name a new feature introduced with PHP4. or co m er The Workshop provides quiz questions to help you solidify your understanding of the cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n Q&A on ly 28 Activity Flick through this book to get an idea of its structure. Think about the topics covered and how they might help you with any future projects. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 29 Hour 2: Installing PHP Overview Before getting started with the PHP language, you must first acquire, install, and works in conjunction with many servers. In this hour, you will learn Where to find PHP and other useful open source software One way of installing PHP on Linux Some configuration directives How to find help when things go wrong Some options that add features to your PHP binary configure the PHP interpreter. PHP is available for a wide range of platforms and PHP is truly cross-platform. It runs on the Windows operating system, most versions of UNIX including Linux, and even the Macintosh. Support is provided for a range of Web servers including Apache (itself open source and cross-platform), Microsoft Personal Web Server. The latter is useful if you want to test your scripts offline on a Windows machine, although Apache can also be run on Windows. You can also compile PHP as a standalone application. You can then call it from the command line. In this book, we will concentrate on building Web applications, but do not underestimate the power of PHP4 as a general scripting tool comparable to Perl. Internet Information Server, WebSite Pro, the iPlanet Web Server, and Microsoft's No PHP is designed to integrate easily with databases. This feature is one of the factors that make the language such a good choice for building sophisticated Web applications. Many databases are directly supported, including Adabas D, InterBase, Solid, dBASE, mSQL, Sybase, Empress, MySQL, Velocis, FilePro, Oracle, UNIX dbm, Informix, and PostgreSQL. PHP also supports ODBC. tf or co m er cia l, http://i-tronics.blogspot.com/ us Platforms, Servers, Databases, and PHP e fo re du ca Which platforms, servers, and databases are supported by PHP4 tio n on ly 30 Throughout this book, we will be using a combination of Linux, Apache, and MySQL. All these are free to download and use, and can be installed relatively easily on a PC. You on can a find Power out PC, more you about can getting find Linux for your about computer LinuxPPC at at . If you want to run Linux information . MySQL, the database we will use in this book, can be downloaded from including UNIX, Windows, and OS/2. . There are versions for many operating systems On the other hand, you can easily stick with Windows, NT, or MacOS. PHP is, after Where to Find PHP and More You can find PHP4 at . PHP4 is open source software, which means that you won't need your credit card handy when you download it. read online at , complete with helpful annotations from other PHP coders. You can also download the manual in several formats. Installing PHP4 for Linux and Apache In this section, we will look at one way of installing PHP4 with Apache on Linux. The process is more or less the same for any UNIX operating system. You might be able to find prebuilt versions of PHP for your system, which are simple to install. Compiling PHP, though, gives you greater control over the features built in to your binary. Before you install you should make sure that you are logged into your system as the No root user. If you are not allowed access to your system's root account, you may need to ask your system administrator to install PHP for you. There are two ways of compiling an Apache PHP module. You can either recompile Apache, statically linking PHP into it, or you can compile PHP as a Dynamic Shared Object (DSO). If your version of Apache was compiled with DSO support, it will be capable of supporting new modules without the need for recompiling the server. tf or co m er cia l, http://i-tronics.blogspot.com/ us e The PHP WebSite is an excellent resource for PHP coders. The entire manual can be fo re du ca all, a cross-platform scripting language. tio n on ly 31 This method is the easiest way to get PHP up and running, and it is the one we will look at in this section. In order to test that Apache supports DSOs you should launch the Apache binary (httpd) with the -l argument. /www/bin/httpd -l You should see a list of modules. If you see mod_so.c Apache. The Apache distribution contains full instructions for this. If you have not already done so, you will need to download the latest distribution of PHP4. Your distribution will be archived as a tar file and compressed with gzip, so you will need to unpack it: tar -xvzf php-4.0.tar.gz After your distribution is unpacked, you should move to the PHP4 distribution arguments that will control the features that PHP will support. For this example, we will include some useful command line arguments, although you might want to available to you later in the hour. ./configure --enable-track-vars \ --with-gd \ --with-mysql \ specify arguments of your own. We will discuss some of the configure options No The path you assign to the --with-apxs argument is likely to be different on your system. It is possible that you will find apxs in the same directory as your Apache executable. After the configure script has run, you can run the make program. You will need a C compiler on your system to run this command successfully. make tf or co --with-apxs=/www/bin/apxs m er cia l, Within your distribution directory you will find a script called configure. This accepts http://i-tronics.blogspot.com/ us cd ../php-4.0 e directory: fo re du ca tio among them, you should be able to proceed; otherwise, you may need to recompile n on ly 32 make install These commands should end the process of PHP4 compilation and installation. You should now be able to configure and run Apache. When we ran the configure script, we included some command-line arguments script itself gives you a list of available options. From the PHP distribution directory type the following: ./configure --help that determined the features that the PHP interpreter will include. The configure The list produced is long, so you may want to add it to a file for reading at leisure: ./configure --help > configoptions.txt Although the output from this command is very descriptive, we will look at a few useful options— especially those that might be needed to follow this book. --enable-track-vars This option automatically populates associative arrays with values submitted as part of GET, POST requests or provided in a cookie. You can read more about arrays in good idea to include this option when running configure. --with-gd Hour 7, "Arrays," and about HTTP requests in Hour 13, "Beyond the Box." It is a --with-gd enables support for the GD library, which, if installed on your system, more about creating dynamic images in Hour 14, "Working with Dynamic Images." You can optionally specify a path to your GD library's install directory: --with-gd=/path/to/dir No tf allows you to create dynamic GIF or PNG images from your scripts. You can read or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly Some configure Options 33 --with-mysql --with-mysql enables support for the MySQL database. If your system has MySQL installed in a directory other than the default location, you should specify a path: --with-mysql=/path/to/dir As you know, PHP provides support for other databases. Table 2.1 lists some of Table 2.1: Some Database configure Options Database Adabas D FilePro msql informix iODBC OpenLink ODBC Oracle PostgreSQL Solid Sybase Sybase-CT Velocis LDAP configure Option --with-adabas --with-filepro --with-msql --with-informix --with-iodbc --with-openlink --with-oracle --with-pgsql --with-solid or tf No Configuring Apache After you have compiled PHP and Apache, you should check Apache's configuration file, httpd.conf, which you will find in a directory called conf in the Apache install directory. Add the following lines to this file: co m er --with-sybase --with-sybase-ct --with-velocis --with-ldap cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n them and the configure options you will need to use them. on ly 34 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps This ensures that the PHP interpreter will parse files that end with the .php extension. Any files with the .phps extension will be output as PHP source. That is, the source code will be converted to HTML and color-coded. This can be useful for debugging your scripts. If you want to offer to your users PHP pages with extensions more familiar to them, the .html extension are treated as PHP files with the following: AddType application/x-httpd-php .html Note that treating files with the .html extension as PHP scripts could slow down your site, because every page with this extension will be parsed by the PHP interpreter before it is served to the user. If PHP has been preinstalled and you have no access to the Apache configuration files, you may be able to change the extensions that will determine which files will called .htaccess. After you have created this file, the directive will affect the enclosing directory, as well as any subdirectories. This technique will only work if the AllowOverride directive for the enclosing directory is set to either FileInfo or All. Although the filename .htaccess is the default for an access control file, it may have you don't have root access, you should be able to read the Apache configuration files. be treated as PHP executables by including an AddType directive in a file An .htaccess file can be an excellent way of customizing your server space if you do not have access to the root account. An additional way of controlling the behavior of PHP, even as a non-root user, is the php.ini file. No php.ini After you have compiled or installed PHP, you can still change its behavior with a file called php.ini. On UNIX systems, the default location for this file is /usr/local/lib; on a Windows system, the default location is the Windows directory. A php.ini file in the current working directory will override one in the default location, so you can change the behavior of PHP on a per-directory basis. tf or co m er been changed. Check the AccessFileName directive in httpd.conf to find out. Even if cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n you can choose any extension you want. You can even ensure that files with on ly 35 You should find a sample php.ini file in your distribution directory, which contains factory settings. Factory settings will be used if no php.ini file is used. The default settings should be adequate for most of the examples in this book, although you can read about some amendments you might like to make in Hour 22, "Debugging." Directives in the php.ini file take the form of a directive and a value separated by an equals sign. Whitespace is ignored. can create one in your script's directory that can override the default. You can also set an environmental variable PHPRC that designates a php.ini file. You can change your php.ini settings at any time, though if you are running PHP as an Apache module, you should restart the server for the changes to take effect. short_open_tag code with the symbols . If this has been disabled, you will see short_open_tag = Off short_open_tag = No short_open_tag = On short_open_tag = True No You can read more about PHP open and close tags in Hour 3, "A First Script." Error Reporting Directives To diagnose bugs in your code, you should enable the directive that allows error messages to be written to the browser. This is on by default: tf short_open_tag = Yes or co To enable the directive you can use one of the following: m er short_open_tag = False cia l, one of the following: http://i-tronics.blogspot.com/ us The short_open_tag directive determines whether you can begin a block of PHP e fo re du ca tio settings in php.ini. Remember, if you are not allowed to alter this document, you n If PHP has been preinstalled on your system, you might want to check some of the on ly 36 display_errors = On You can also set the level of error reporting. We will cover the options available for the error_reporting directive in more depth in Hour 22. For now, however, you should set this to the following: error_reporting = E_ALL & ~ E_NOTICE This will report all errors but not notices that warn about potential problems with your code. Notices can interfere with some PHP techniques. This setting is the default. Variable Directives PHP makes certain variables available to you as a result of a GET request, a POST request, or a cookie. You can influence this in the php.ini file. The track_vars directive creates associative arrays containing elements generated as a result of an HTTP request. This is allowed by default: The register_globals directive determines whether values resulting from an HTTP request should be made available as global variables. Many scripts in this book will require the following setting to be enabled: register_globals = On Help! Help is always at hand on the Internet, particularly for problems concerning open matter how intractable your installation, configuration, or programming problem might seem, chances are you are not alone. Someone will have already answered your question. When you hit a brick wall, your first recourse should be to the official PHP site at , particularly the annotated manual at . If you still can't find your answer, don't forget that the PHP site is searchable. The advice you are seeking may be lurking in a press release or a Frequently Asked No tf or source software. Wait a moment before you hit the send button, however. No co m er cia l, http://i-tronics.blogspot.com/ us e track_vars = On fo re du ca tio n on ly 37 Questions file. Another excellent and searchable resource is the PHP Knowledge Base at . Still no luck? You can find links to searchable mailing list archives at . These archives represent a huge information resource with contributions from many of the great and the good in the If you are still convinced that your problem has not been addressed, you may well be doing the PHP community a service by exposing it. Although these lists are often high volume, you can learn a lot from them. If you are serious about PHP scripting, you should certainly subscribe at least to a digest list . Once subscribed to the list that matches your concerns, you might consider posting your problem. When you post a question it is often a good idea to include as much information as possible (without writing a novel). The following items often are pertinent: Your operating system The configure options you chose failure Any output from the configure or make commands that preceded an installation A reasonably complete example of the code that is causing you problems research skills will stand you in good stead. A good researcher can generally solve a problem quickly and efficiently. Asking a naive question of a technical list often involves a wait rewarded only by a message or two referring you to the archives where you should have begun your search for answers. Second, remember that a mailing list is not analogous to a technical support call center. No one is paid to answer your questions. Despite this, you have access to an No impressive resource of talent and knowledge, including that of some of the creators of PHP itself. A good question and its answer will be archived to help other coders. Asking a question that has been answered several times just adds more noise. Having said this, don't be afraid to post a problem to the list. PHP developers are a civilized and helpful breed, and by bringing a problem to the attention of the community, you might be helping others to solve the same problem. tf or co m er Why all these cautions about posting a question to a mailing list? First, developing cia l, http://i-tronics.blogspot.com/ us e The version of PHP you are running or installing fo re du ca tio You can join the PHP mailing lists at . n on ly PHP community. Spend some time trying out a few keyword combinations. 38 Summary PHP4 is open source software. It is also open in the sense that it does not demand that you use a particular server, operating system, or database. In this hour, you learned where to locate PHP and other open source software that can help you host and serve Web sites. You learned how to compile PHP as an distribution will contain step-by-step instructions. You learned some of the configure options that can change the features that your binary will support. You about sources of support. You should now be ready to come to grips with the language itself. learned about php.ini and some of the directive it contains. Finally, you learned Apache module on Linux. If you download a PHP binary for another platform, your Q&A Q You have covered an installation for Linux and Apache. Does that mean A No, one of PHP's great strengths is that it runs on multiple platforms. If you are don't forget to read the files that come with your PHP distribution. You should find comprehensive step-by-step instructions for installation. If you are still having problems, review the "Help!" section earlier in this hour. The online resources mentioned there will almost certainly contain the answers you need. No material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. Quiz Where can you find the PHP online manual? From a UNIX operating system, how would you get help on configuration options (the options that you pass to the configure script in your PHP distribution)? http://i-tronics.blogspot.com/ tf The Workshop provides quiz questions to help you solidify your understanding of the or Workshop co m er cia l, having trouble installing PHP to work on your operating system or with your server, us that this book will not apply to my server and operating system? e fo re du ca tio n on ly 39 What is Apache's configuration file typically called? What line should you add to the Apache configuration file to ensure that the .php extension is recognized? What is PHP's configuration file called? Activity Install PHP on your system. If it is already in place, review your php.ini file and check your configuration. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 40 Hour 3: A First Script Overview Having installed and configured PHP, it is now time to put it to the test. In this hour, you will create your first script and spend a little time analyzing its syntax. By the end of the hour, you should be ready to create documents that include both HTML In this hour, you will learn How to create, upload, and run a PHP script How to incorporate HTML and PHP in the same document How to make your code clearer with comments Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI and Emacs on UNIX operating systems. Most popular HTML editors provide at least some support for PHP. Type in the example in Listing 3.1 and save the file, calling it something like first.php. Listing 3.1: A First PHP Script 1: Figure 3.1 shows the script created in Listing 3.1 as typed into the BBEdit text editor for MacOS. tf or print "Hello Web!"; co m er cia l, HTML documents, PHP files are made up of plain text. You can create them with any http://i-tronics.blogspot.com/ us Our First Script e fo re du ca and PHP. tio n on ly 41 Figure 3.1: Your first script as created in the BBEdit text editor. the file as PHP code and invoke the interpreter. The default PHP extension for a PHP 4 document is .php. This can be changed, however, by altering the server's configuration. You saw how to do this in Hour 2, "Installing PHP." If you are not working directly on the machine that will be serving your PHP script, you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for MacOS to upload your saved document to the server. from the first.php script. Figure 3.2: Success: the output from Listing 3.1. No If PHP is not installed on your server or your file's extension is not recognized, you may not see the output shown in Figure 3.2. In these cases, you probably will see the source code created in Listing 3.1. Figure 3.3 shows what happens when an unknown extension is encountered. tf or co m er cia l, http://i-tronics.blogspot.com/ us all has gone well, you should see the script's output. Figure 3.2 shows the output e After the document is in place, you should be able to access it via your browser. If fo re du ca tio The extension to the PHP document is important because it tells the server to treat n on ly 42 Figure 3.3: Failure: the extension is not recognized. If this happens, first check the extension with which you saved your PHP script. In Figure 3.3, the document was accidentally called first.nphp. If the file extension is as it should be, you may need to check that PHP has been installed properly and that your server is configured to work with the extension that you have used for your script. You can read more about installing and configuring PHP in Hour 2. Now that you have uploaded and tested your script, you can take a look at the code Beginning and Ending a Block of PHP Statements When writing PHP, you need to inform the interpreter that you want it to execute and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP code. Table 3.1: PHP Start and End Tags tags No Short tags ASP tags Script tags tf Standard or Tag Style co m er your commands. If you don't do this, the code you write will be mistaken for HTML Start Tag cia l, http://i-tronics.blogspot.com/ us e in a little more detail. End Tag ?> ?> %> fo re du ca tio n on ly 43 Of the tags in Table 3.1, only the standard and the script tags can be guaranteed to work on any configuration. The short and ASP style tags must be explicitly enabled in your php.ini. You examined the php.ini file in Hour 2. To activate recognition for short tags, you must make sure that the short_open_tag switch is set to "On" in php.ini: short_open_tag = On; Short tags are enabled by default, so you would only need to edit php.ini if you want To activate recognition for the ASP style tags, you must enable the asp_tags asp_tags = On; After you have edited php.ini, you should be able to choose from any of the four styles for use in your scripts. This is largely a matter of preference, although if you intend to work with XML, you should disable the short tags ( ) and work with the standard tags (). Let's run through some of the ways in which you can legally write the code in Listing 3.1. You could use any of the four PHP start and end tags that you have seen: print("Hello Web!"); ?> <% No %> Single lines of code in PHP also can be presented on the same line as the PHP start and end tags: tf print("Hello Web!"); or co m er cia l, Now that you know how to define a block of PHP code, take a closer look at the code in Listing 3.1 itself. The print() Function print() is a function that outputs data. In most cases, anything output by print() ends up in the browser window. A function is a command that performs an action, usually modified in some way by data provided for it. Data sent to a function is almost always placed in parentheses after the function name. In this case, you sent enclosed by quotation marks, either single or double. the print() function a collection of characters, or string. Strings must always be Note Function calls generally require parentheses after their name whether or not they demand that data be passed to them. print() is an exception, and enclosing the data you want to print to the browser in parentheses is optional. This is the more common syntax, so we will usually omit the brackets in our examples. informs the interpreter that you have completed a statement. TERM is to PHP what a sentence is to written or spoken English. A statement should usually end with a semicolon; a sentence should enclose other statements, and statements that end a block of code. In most cases, however, failure to end a statement with a semicolon end with a period. Exceptions to this include statements that semicolon is optional. No Combining HTML and PHP The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML document simply by adding HTML outside the PHP start and end tags, as shown in Listing 3.2. Listing 3.2: A PHP Script Including HTML 1: 2: tf or Because the statement in Listing 3.1 is the final one in that block of code, the co will confuse the interpreter and result in an error. m er cia l, NEW A statement represents an instruction to the interpreter. Broadly, it http://i-tronics.blogspot.com/ us You ended your only line of code in Listing 3.1 with a semicolon. The semicolon e fo re du ca tio n on ly 45 3: Listing 3.2 A PHP script including HTML 4: 5: 6: 7: 10: 11: 12: print "hello world"; typing in the code. The PHP interpreter ignores everything outside PHP open and close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4, you would see the string "hello world" in bold. If you were to view the document source, as shown in Figure 3.5, the listing would look exactly like a normal HTML document. You can include as many blocks of PHP code as you need in a single document, code in a single document, they combine to form a single script. Anything defined in the first block (variables, functions, or classes, for example) usually will be available to subsequent blocks. No Figure 3.4: The output of Listing 3.2 as viewed in a browser. tf or co m er cia l, http://i-tronics.blogspot.com/ us e interspersing them with HTML as required. Although you can have multiple blocks of fo re du ca As you can see, incorporating HTML into a PHP document is simply a matter of tio n on ly 46 Figure 3.5: The output of Listing 3.2 as HTML source code. Adding Comments to PHP Code Code that seems clear at the time of writing, can seem like a hopeless tangle when can save you time later on and make it easier for other programmers to work with your code. NEW TERM A comment is text in a script that is ignored by the interpreter. Comments can be used to make code more readable, or to annotate a script. All text from either of these marks until either the end of the line or the PHP close tag is ignored. # // this is a comment this is another comment Multiline comments begin with a forward slash followed by an asterisk (/*) and end /* No this is a comment none of this will be parsed by the interpreter * tf with an asterisk followed by a forward slash (*/). / or co m er Single line comments begin with two forward slashes (/ /) or a single hash sign (#). cia l, http://i-tronics.blogspot.com/ us e you come to amend it six months later. Adding comments to your code as you write fo re du ca tio n on ly 47 Summary You should now have the tools at your disposal to run a simple PHP script on a properly configured server. In this hour, you created your first PHP script. You learned how to use a text editor use to begin and end blocks of PHP code. You learned how to use the print() function to send data to the browser, and you brought HTML and PHP together into the same documents. script. Finally, you learned about comments and how to add them to PHP Q&A Q Which are the best start and end tags to use? A It is largely a matter of preference. For the sake of portability the standard tags () are probably the safest bet. Short tags are enabled by default and have the virtue of brevity. example). Even if you save files created using this type of editor in plain text format, hidden characters are likely to creep into your code. Q When should I comment my code? A This is a matter of preference once again. Some short scripts will be self-explanatory to you, even after a long interval. For scripts of any length or complexity, you should comment your code. This often saves you time and frustration in the long run. No Workshop The Workshop provides quiz questions to help you solidify your understanding of the material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. tf or co m er cia l, A Do not use word processors that format text for printing (such as Word, for http://i-tronics.blogspot.com/ us Q What editors should I avoid when creating PHP code? e fo re du ca tio n on ly to create and name a PHP document. You examined four sets of tags that you can 48 Quiz Can a user read the source code of PHP script you have successfully installed? What do the standard PHP delimiter tags look like? What do the ASP PHP delimiter tags look like? What do the script PHP delimiter tags look like? What function would you use to output a string to the browser? Activity scripts. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca Familiarize yourself with the process of creating, uploading, and running PHP tio n on ly 49 Hour 4: The Building Blocks Overview In this hour, you are going to get your hands dirty with some of the nuts and bolts of the language. bombarded with information. Don't worry— you can always refer back here later on. If you're already an experienced programmer, you should at least skim this hour's lesson. It covers a few PHP-specific features. In this hour, you will learn How to define and access variables About data types About variables— what they are and how to use them How to use operators to create expressions Variables A variable is a special container that you can define to "hold" a value. A variable consists of a name that you can choose, preceded by a dollar ($) sign. The variable names cannot include spaces or characters that are not alphanumeric. The following code defines some legal variables: $a; $a_longish_variable_name; name can include letters, numbers, and the underscore character (_). Variable No $2453; $sleepyZZZZ Remember that a semicolon (;) is used to end a PHP statement. The semicolons in the previous fragment of code are not part of the variable names. NEW TERM A variable is a holder for a type of data. It can hold numbers, strings of characters, objects, arrays, or booleans. The contents of a variable can be changed at any time. tf or co m er cia l, How to define and use constants http://i-tronics.blogspot.com/ us About some of the more commonly used operators e fo re du ca Concentrate on understanding rather than memorizing the features covered. tio n There's a lot of ground to cover, and if you are new to programming, you might feel on ly 50 As you can see, you have plenty of choices about naming, although it is unusual to see a variable name that consists exclusively of numbers. To declare a variable, you need only to include it in your script. You usually declare a variable and assign a value to it in the same statement. $num1 = 8; $num2 = 23; The preceding lines declare two variables, using the assignment operator (=) to give them values. You will learn about assignment in more detail in the Operators and Expressions section later in the hour. After you give your variables values, you print $num1; is equivalent to print 8; as long as $num1 contains 8. Dynamic Variables As you know, you create a variable with a dollar sign followed by a variable name. Unusually, the variable name can itself be stored in a variable. So, when assigning $user = "bob"; is equivalent to $holder="user"; $$holder = "bob"; a value to a variable The $holder variable contains the string "user", so you can think of $$holder as a Note You can use a string constant to define a dynamic variable instead of a variable. To do so, you must wrap the string you want to use for the variable name in braces: ${"user"} = "bob"; No When accessing a dynamic variable, the syntax is exactly the same: $user ="bob"; print $user; is equivalent to $user ="bob"; $holder="user"; tf or This might not seem useful at first glance. However, by using the concatenation operator and a loop (see Hour 5, "Going with the Flow"), you can use this technique to create tens of variables dynamically. co m er dollar sign followed by the value of $holder. PHP interprets this as $user. cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n can treat them exactly as if they were the values themselves. In other words on ly 51 print $$holder; If you want to print a dynamic variable within a string, however, you need to give the interpreter some help. The following print statement: $user="bob"; $holder="user"; print "$$holder"; does not print "bob" to the browser as you might expect. Instead it prints the strings "$" and "user" together to make "$user". When you place a variable within quotation marks, PHP helpfully inserts its value. In this case, PHP replaces $holder that a variable within a string is part of a dynamic variable, you must wrap it in $user="bob"; $holder="user"; print "${$holder}"; now prints "bob", which is the value contained in $user. Listing 4.1 brings some of the previous code fragments together into a single script using a string stored in a variable to initialize and access a variable called $user. 1: 2: 4: 5: 6: "; 15: print $$holder; 16: print "
"; tf or co 17: print "${$holder}
"; // prints "bob" 18: print "${'user'}
"; 19: ?> // prints "bob" m er cia l, 3: Listing 4.1 Dynamically setting and accessing variables // prints "bob" // prints "bob" http://i-tronics.blogspot.com/ us e fo re du ca braces. The print statement in the following fragment: tio n with the string "user". The first dollar sign is left in place. To make it clear to PHP on ly 52 20: 21: References to Variables By default, variables are assigned by value. In other words, if you were to assign $aVariable to $anotherVariable, a copy of the value held in $aVariable would be stored in $anotherVariable. Subsequently changing the value of $aVariable would have no effect on the contents of $anotherVariable. Listing 4.2 illustrates this. Listing 4.2: Variables Are Assigned by Value 1: 2: 4: 5: 6: 13: 14: 3: Listing 4.2 Variables are assigned by value 9: // a copy of the contents of $aVariable is placed in $anotherVariable 11: print $anotherVariable; // prints 42 assigned to $anotherVariable. A copy of the value of $aVariable is placed in $anotherVariable. Changing the value of $aVariable to 325 has no effect on the contents of $anotherVariable. The print statement demonstrates this by outputting 42 to the browser. In PHP4, you can change this behavior, forcing a reference to $aVariable to be Listing 4.3. No Listing 4.3: Assigning a Variable by Reference 1: 2: 3: Listing 4.3 Assigning a variable by reference 4: 5: tf assigned to $anotherVariable, rather than a copy of its contents. This is illustrated in or co m er This example initializes $aVariable, assigning the value 42 to it. $aVariable is then cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 53 6: 13: 14: We have added only a single character to the code in Listing 4.2. Placing an variable, rather than a copy of its contents, is assigned to $anotherVariable. Now any changes made to $aVariable are seen when accessing $anotherVariable. In other words, both $aVariable and $anotherVariable now point to the same value. Because this technique avoids the overhead of copying values from one variable to another, it can result in a small increase in performance. Unless your script assigns variables intensively, however, this performance gain will be barely measurable. Note References to variables were introduced with PHP4. Different types of data take up different amounts of memory and may be treated therefore demand that the programmer declare in advance which type of data a variable will contain. PHP4 is loosely typed, which means that it will calculate data it means that variables can be used flexibly, holding a string at one point and an you expect a variable to hold one data type when in fact it holds something completely different. types as data is assigned to each variable. This is a mixed blessing. On the one hand, integer at another. On the other hand, this can lead to confusion in larger scripts if No Table 4.1 shows the six data types available in PHP4. Table 4.1: Data Types Example 5 Description A number whole Type Integer tf or co m er differently when they are manipulated in a script. Some programming languages cia l, Data Types http://i-tronics.blogspot.com/ us e fo re du ca ampersand (&) in front of the $aVariable variable ensures that a reference to this tio n on ly 54 Double 3.234 A floating-point number String "hello" A collection of characters special values true or false Object See Hour 8, "Arrays" Of PHP4's six data types, we will leave arrays and objects for Hours 7 and 8. You can use PHP4's built-in function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type. Listing 4.4 assigns four different data types to a single variable, testing it with gettype() each time. Note You can read more about calling functions in Hour 6, "Functions." Listing 4.4: Testing the Type of a Variable 1: 2: 3: Listing 4.3 Testing the type of a variable 4: 5: 6: "); 13: $testing = 5.0; tf 8: print gettype( $testing ); // integer 9: print "
"; or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca Array See Hour 7, tio "Objects" n on ly Boolean true One of the 55 14: print gettype( $testing ); // double 15: print("
"); 16: $testing = true; 17: print gettype( $testing ); // boolean 18: print "
"; 19: ?> 20: 21: integer string double boolean strings in your scripts, they should always be surrounded by double (") or single (') includes a decimal point. A boolean can be one of two special values, true or false. Note Prior to PHP4, there was no boolean type. Although true was used, it actually resolved to the integer 1. Changing Type with settype() between the parentheses and separated by commas. Listing 4.5 converts 3.14 (a No double) to the four types that we are covering in this hour. Listing 4.5: Changing the Type of a Variable with settype() 1: 2: 3: Listing 4.5 Changing the type of a variable with settype() 4: http://i-tronics.blogspot.com/ tf settype(), you must place the variable to change (and the type to change it to) or PHP provides the function settype() to change the type of a variable. To use co m er cia l, quotation marks. A double is a floating-point number. That is, a number that us without a decimal point. A string is a collection of characters. When you work with e An integer is a whole or real number. In simple terms, it can be said to be a number fo re du ca This script produces the following: tio n on ly 56 5: 6: "; // 3.14 10: settype( $undecided, string ); 11: print gettype( $undecided ); // string 12: print " -- $undecided
"; // 3.14 13: settype( $undecided, integer ); 14: print gettype( $undecided ); // integer 15: print " -- $undecided
"; // 3 16: settype( $undecided, double ); 17: print gettype( $undecided ); // double 18: print " -- $undecided
"; // 3.0 19: settype( $undecided, boolean ); 21: print " -- $undecided
"; 22: ?> 23: 24: print the value of the variable $undecided to the browser. When we convert the That's why $undecided still contains 3 after we have changed it back to a double. Finally, we convert $undecided to a boolean. Any number other than 0 becomes true No when converted to a boolean. When printing a boolean in PHP, true is represented as 1 and false as an empty string, so $undecided is printed as 1. tf or string "3.14" to an integer, any information beyond the decimal point is lost forever. co In each case, we use gettype() to confirm that the type change worked and then m er cia l, http://i-tronics.blogspot.com/ us 20: print gettype( $undecided ); // boolean // 1 e fo re du ca tio n on ly 57 Changing Type by Casting By placing the name of a data type in brackets in front of a variable, you create a copy of that variable's value converted to the data type specified. The principal leaving the original variable untouched. Listing 4.6 illustrates this. Listing 4.6: Casting a Variable 1: 2: 3: Listing 4.6 Casting a variable 4: 5: 6: "; // 3.14 difference between settype() and a cast is the fact that casting produces a copy, 11: $holder = ( string ) $undecided; 12: print gettype( $holder ); 13: print " -- $holder
"; 14: $holder = ( integer ) $undecided; 16: print " -- $holder
"; co 15: print gettype( $holder ); m er 17: $holder = ( double ) $undecided; 18: print gettype( $holder ); tf or No 19: print " -- $holder
"; 20: $holder = ( boolean ) $undecided; // boolean // 1 21: print gettype( $holder ); 22: print " -- $holder
"; 23: ?> cia l, // string // 3.14 // integer // 3 // double // 3.14 http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 58 24: 25: We never actually change the type of $undecided, which remains a double throughout. In fact, by casting $undecided, we create a copy that is then converted to the type we specify. This new value is then stored in the variable $holder. Because we are working with a copy of $undecided, we never discard any information from it as we did in Listing 4.5. Operators and Expressions You can now assign data to variables. You can even investigate and change the data type of a variable. A programming language isn't very useful, though, unless you can manipulate the data you can store. Operators are symbols that make it possible to use one or more values to produce a new value. A value that is operated on by an operator is referred to as an operand. NEW TERM NEW TERM An operator is a symbol or series of symbols that, when used in conjunction with values, performs an action and usually produces a new value. An operand is a value used in conjunction with an operator. There are usually two operands to one operator. Let's combine two operands with an operator to produce a new value: 4+5 4 and 5 are operands. They are operated on by the addition operator (+) to produce 9. Operators almost always sit between two operands, though you will see a few exceptions later in this hour. The combination of operands with an operator to manufacture a result is called an expression. Although most operators form the basis of expressions, an expression No need not contain an operator. In fact in PHP, an expression is defined as anything that resolves to a value. This includes integer constants such as 654, variables such as $user, and function calls such as gettype(). The expression (4 + 5), therefore is an expression that consists of two further expressions and an operator. NEW TERM An expression is any combination of functions, values, and operators that resolve to a value. As a rule of thumb, if you can use it as if it were a value, it is an expression. tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 59 Now that we have the principles out of the way, it's time to take a tour of PHP4's more common operators. The Assignment Operator consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand: $name ="matt"; The variable $name now contains the string "matt". Interestingly, this construct is changes the variable $name without producing a value, but in fact, a statement that uses the assignment operator always resolves to a copy of the value of the right operand. Thus print ( $name = "matt" ); prints the string "matt" to the browser in addition to assigning "matt" to $name. Arithmetic Operators The arithmetic operators do exactly what you would expect. Table 4.2 lists these operators. The addition operator adds the right operand to the left operand. The operator divides the left-hand operand by the right. The multiplication operator multiplies the left-hand operand by the right. The modulus operator returns the remainder of the left operand divided by the right. Table 4.2: Arithmetic Operators Operator + − subtraction operator subtracts the right-hand operand from the left. The division or co m er Name cia l, us Example 10+3 10− 3 10/3 10*3 10%3 e tf No Addition Subtraction Division Multiplication Modulus / * % http://i-tronics.blogspot.com/ fo re du ca Example Result 13 7 30 1 an expression. It might look at first glance that the assignment operator simply 3.3333333333333 tio n on ly You have met the assignment operator each time we have initialized a variable. It 60 The Concatenation Operator The concatenation operator is a single dot. Treating both operands as strings, it appends the right-hand operand to the left. So "hello"." world" returns "hello world" Regardless of the data types of the operands, they are treated as strings, and the More Assignment Operators Although there is really only one assignment operator, PHP4 provides a number of combination operators that transform the left-hand operand as well as return a Assignment operators break this rule. A combined assignment operator consists of a standard operator symbol followed by an equals sign. Combination assignment operators save you the trouble of using two operators yourself. For example, $x = 4; result. As a rule, operators use their operands without changing their values. is equivalent to $x = 4; $x = $x + 4; // $x now equals 8 There is an assignment operator for each of the arithmetic operators and one for the Table 4.3: Some Combined Assignment Operators Example $x += 5 $x − = 5 $x /= 5 Equivalent to $x = $x + 5 $x = $x − 5 $x = $x / 5 No Operator += −= /= tf concatenation operator. Table 4.3 lists some of the most common. or co m er $x += 4; // $x now equals 8 cia l, http://i-tronics.blogspot.com/ us e fo re du ca result always is a string. tio n on ly 61 * = $x *= 5 $x%=5 $x .= "test" $x = $x * 5 %= .= $x = $x % 5 $x = $x" test" Each of the examples in Table 4.3 transforms the value of $x using the value of the right-hand operand. Comparison Operators Comparison operators perform tests on their operands. They return the boolean value true if the test is successful, or false otherwise. This type of expression is useful in control structures, such as if and while statements. You will meet these in Hour 5. To test whether the value contained in $x is smaller than 5, for example, you would use the less than operator: $x < 5 contained 7, the expression would resolve to false. Table 4.4 lists the comparison operators. Table 4.4: Comparison Operators Operator m er Name cia l, us If $x contained 3, this expression would be equivalent to the value true. If $x e Returns True if Left equivalent to right Left is not equivalent to right is fo re du ca Example $x == 5 $x != 5 is $x === 5 No === tf != or co == Equivalence Non-equivalence Identical Left equivalent to right and they are the same type http://i-tronics.blogspot.com/ tio n Result false true false on ly 62 > Greater Left is than greater than right $x >4 false >= Greater equal to than or Left greater than equal right is or to $x >= 4 true than right <= Less than or equal to Left is less than equal right fo re du ca $x <= 4 or to These operators are most commonly used with integers or doubles, although the equivalence operator is also used to compare strings. Creating More Complex Test Expressions with the Logical Operators The logical operators test combinations of booleans. The or operator, for example returns true if either the left or the right operand is true. true || false would return true. The and operator only returns true if both the left and right operands are true. would return false. It's unlikely that you would use a logical operator to test boolean No constants, however. It would make more sense to test two or more expressions that resolve to a boolean. For example, ( $x > 2 ) && ( $x < 15 ) would return true if $x contained a value that is greater than 2 and smaller than 15. We include the parentheses to make the code easier to read. Table 4.5 lists the logical operators. tf or true && false co m er cia l, http://i-tronics.blogspot.com/ us e tio < Less than Left is less x<4 n true on ly false 63 Table 4.5: Logical Operators Operator Name Returns True if… || Or Left or right is true or Or Left or right is true xor Xor Left or right is true but not both && And Left right true and And Left right true ! Not The and are true && false true || true false true || false true true || false true Example Result and are operand not true Why are there two versions of both the or and the and operators? The answer lies in Automatically Incrementing and Decrementing an Integer Variable integer variable. You will usually need to do this when you are counting the iterations of a loop. You have already learned two ways of doing this. I could No increment the integer contained by $x with the addition operator $x = $x + 1; // $x is incremented or with a combined assignment operator $x += 1; // $x is incremented tf or When coding in PHP, you will often find it necessary to increment or decrement an co m er operator precedence, which you will look at later in this section. cia l, http://i-tronics.blogspot.com/ us single is e fo re du ca false true && false false ! true false tio n on ly 64 In both cases, the resultant integer is assigned to $x. Because expressions of this kind are so common, PHP provides some special operators that allow you to add or subtract the integer constant 1 from an integer variable, assigning the result to the variable itself. These are known as the post-increment and post-decrement operators. The post-increment operator consists of two plus symbols appended to a variable name. $x++; // $x is incremented increments the variable $x by one. Using two minus symbols in the same way $x− − ; // $x is decremented If you use the post-increment or post-decrement operators in conjunction with a conditional operator, the operand will only be modified after the test has been completed: $x = 3; $x++ < 4; // true incremented. In some circumstances, you might want to increment or decrement a variable in a test expression before the test is carried out. PHP provides the pre -increment and exactly the same way as the post-increment and post-decrement operators. They are written with the plus or minus symbols preceding the variable: pre-decrement operators for this purpose. On their own, these operators behave in − − $x; // $x is decremented If these operators are used as part of a test expression, the incrementation occurs before the test is carried out. No $x = 3; ++$x < 4; // false In the previous fragment, $x is incremented before it is tested against 4. The test expression returns false because 4 is not smaller than 4. tf or co ++$x; // $x is incremented m er cia l, http://i-tronics.blogspot.com/ us operator, so the test expression returns true. After this test is complete, $x is e In the previous example, $x contains 3 when it is tested against 4 with the less than fo re du ca tio n decrements the variable: on ly 65 Operator Precedence When you use an operator, the interpreter usually reads your expression from left to right. For complex expressions that use more than one operator, though, the waters can become a little murky. First, consider a simple case: 4+5 4+5 * 2 This presents a problem. Does it mean the sum of 4 and 5, which should then be multiplied by 2, giving the result 18? Does it mean 4 plus the result of 5 multiplied by 2, resolving to 14? If you were to read simply from left to right, the former would be true. In fact, PHP attaches different precedence to operators. Because the multiplication operator has higher precedence than the addition operator does, the second solution to the problem is the correct one. expression with parentheses: (4+5) * Whatever the precedence of the operators in a complex expression, it is a good idea Table 4.6 lists the operators covered in this hour in precedence order (highest first). Table 4.6: Order of Precedence for Selected Operators Operators No +− < <= => > == === != && || tf / *% or ++ − − (cast) co m er to use parentheses to make your code clearer and to save you from obscure bugs. cia l, 2 http://i-tronics.blogspot.com/ us You can force PHP to execute the addition expression before the multiplication e fo re du ca tio fragment? n There's no room for confusion, here. PHP simply adds 4 to 5. What about the next on ly 66 = += − = /= *=%= .= and xor or As you can see, or has a lower precedence than || and and has a lower precedence than &&, so you could use the lower-precedence logical operators to change the way two expressions are equivalent, but the second is much easier to read: $x and $y || $z ( $x && $y ) || $z a complex test expression is read. This is not necessarily a good idea. The following Constants Variables offer a flexible way of storing data. You can change their values and the type of data they store at any time. If, however, you want to work with a value that you do not want to alter throughout your script's execution, you can define a constant. You must use PHP's built-in function define() to create a constant. After you have done this, the constant cannot be changed. To use the define() function, you must place the name of the constant and the value you want to give it within the call's parentheses: The value you want to set can only be a number or a string. By convention, the constant name only; no dollar symbol is required. Listing 4.7 defines and accesses Listing 4.7: Defining a Constant No tf 1: 2: 3: Listing 4.7 Defining a constant 4: 5: 6: 10: 11: Notice that we used the concatenation operator to append the value held by our distinguishing between a constant and a string within quotation marks. Predefined Constants PHP automatically provides some built-in constants for you. __FILE__, for example, returns the name of the file currently being read by the interpreter. __LINE__ returns the line number of the file. These constants are useful for generating error messages. You can also find out which version of PHP is interpreting the script with PHP_VERSION. This can be useful if you want to limit a script to run on a particular Summary learned about variables and how to assign to them using the assignment operator. You learned about dynamic or "variable" variables. You also learned how to assign to variables by reference rather than by value. You were introduced to operators and learned how to combine some of the most common of these into expressions. No Q&A Q Why can it be useful to know the type of data a variable holds? A Often the data type of a variable constrains what you can do with it. You may want to make sure that a variable contains an integer or a double before using it in a mathematical calculation, for example. tf or Finally, you learned how to define and access constants. co m er In this hour, you covered some of the basic features of the PHP language. You cia l, http://i-tronics.blogspot.com/ us PHP release. e fo re du ca tio n constant to the string "Welcome". This is because the interpreter has no way of on ly 68 You explore situations of this kind a little further in Hour 16, "Working with Data." Q Should I obey any conventions when naming variables? A Your goal should always be to make your code both easy to read and understand. A variable such as $ab123245 tells you nothing about its role in your script and invites typos. Keep your variable names short and descriptive. A variable named $f is unlikely to mean much to you when you return to your code after a month or so. A variable named $filename, on the other hand, should make more sense. Q Should I learn the operator precedence table? A There is no reason why you shouldn't, but I would save the effort for more useful tasks. By using parentheses in your expressions, you can make your code easy to read at the same time as defining your own order of precedence. Workshop The Workshop provides quiz questions to help you solidify your understanding of the material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. Quiz Which of the following variable names is not valid? $a_value_submitted_by_a_user $666666xyz $xyz666666 No $the first $file-name How could you use the string variable created in the assignment expression $my_var = "dynamic"; to create a "variable" variable, assigning the integer 4 to it. How might you access this new variable? tf $_____counter____ or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 69 What will the following statement output? print gettype("4"); What will be the output from the following code fragment? $test_val = 5.4566; settype( $test_val, "integer" ); print $test_val; Which of the following statements does not contain an expression? 4; gettype(44); 5/12; Which of the statements in question 5 contains an operator? What value will the following expression return? 5<2 What data type will the returned value be? Activities Create a script that contains at least five different variables. Populate them with the browser. first value is Assign values to two variables. Use comparison operators to test whether the The same as the second Less than the second Less than or equal to the second Print the result of each test to the browser. Change the values assigned to your test variables and run the script again. No tf or co Greater than the second m er cia l, values of different data types and use the gettype() function to print each type to http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 70 Hour 5: Going with the Flow Overview The scripts created in the last hour flow only in a single direction. The same leave much room for flexibility. statements are executed in the same order every time a script is run. This does not You now will look at some structures that enable your scripts to adapt to How to use the if statement to execute code only if a test expression evaluates to true statement evaluates to false a test expression How to execute alternative blocks of code when the test expression of an if How to use the switch statement to execute code based on the value returned by How to repeat execution of code using a while statement How to break out of loops Most scripts evaluate conditions and change their behavior accordingly. The facility according to circumstances. Like most programming languages, PHP4 allows you to The if Statement No The if statement evaluates an expression between parentheses. If this expression results in a true value, a block of code is executed. Otherwise, the block is skipped entirely. This enables scripts to make decisions based on any number of factors. if ( expression ) { tf or do this with an if statement. co to make decisions makes your PHP pages dynamic, capable of changing their output m er Switching Flow cia l, How to nest one loop within another http://i-tronics.blogspot.com/ us How to use for statements to make neater loops e fo re du ca circumstances. In this hour, you will learn tio n on ly 71 // code to execute if the expression evaluates to true } Listing 5.1 executes a block of code only if a variable contains the string "happy". Listing 5.1: An if Statement 2: 3: Listing 5.1 4: 5: 6: 13: 14: { print "Hooray, I'm in a good mood"; } You use the comparison operator == to compare the variable $mood with the string "happy". If they match, the expression evaluates to true, and the code block below example, this is only necessary if the block contains more than one line. The the if statement is executed. Although the code block is wrapped in braces in the following fragment, therefore, would be acceptable: if ( $mood == "happy" ) No If you change the value of $mood to "sad" and run the script, the expression in the if statement evaluates to false, and the code block is skipped. The script remains sulkily silent. tf or print "Hooray, I'm in a good mood"; co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 1: 72 Using the else Clause with the if Statement When working with the if statement, you will often want to define an alternative block of code that should be executed if the expression you are testing evaluates to of code: if ( expression ) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } false. You can do this by adding else to the if statement followed by a further block executed if $mood is not equivalent to "happy". Listing 5.2: An if Statement That Uses else 1: 2: 3: Listing 5.2 4: 5: 6: 17: 18: print "Not happy but $mood"; } $mood contains the string "sad", which is not equivalent to "happy", so the code is skipped. The block of code after else, therefore, is executed, and the message "Not happy but sad" is printed to the browser. Using the else clause with the if statement allows scripts to make sophisticated decisions, but you currently are limited to an either -or branch. PHP4 allows you to evaluate multiple expressions in a cascade. Using the elseif Clause with the if Statement default block of code: if ( expression ) { } { No else If the first expression does not evaluate to true, then the first block of code is ignored. The elseif clause then causes another expression to be evaluated. Once tf // and this one evaluates to true { // code to execute in all other cases } or // code to execute if the previous expression failed co elseif ( another expression ) m er // code to execute if the expression evaluates to true cia l, http://i-tronics.blogspot.com/ us You can use an if-elseif-else construct to test multiple expressions before offering a e fo re du ca tio n expression in the if statement evaluates to false. This means that the first block of on ly 74 again, if this expression evaluates to true, then the second block of code is executed. Otherwise, the block of code associated with the else clause is executed. You can include as many elseif clauses as you want, and if you don't need a default action, you can omit the else clause. Listing 5.3 adds an elseif clause to the previous example. 1: 2: 3: Listing 5.3 4: 5: 6: 21: 22: tf or { print "Neither happy nor sad but $mood"; } co m er cia l, 11: } http://i-tronics.blogspot.com/ us e print "Hooray, I'm in a good mood"; fo re du ca tio n on ly Listing 5.3: An if Statement That Uses else and elseif 75 Once again, $mood holds a string, "sad". This is not equivalent to "happy", so the first block is ignored. The elseif clause tests for equivalence between the contents of $mood and "sad", which evaluates to true. This block of code is therefore executed. The switch Statement The switch statement is an alternative way of changing program flow according to the evaluation of an expression. There are some key differences between the switch different code according to the result of that expression, as long as the expression evaluates to a simple type (a number, a string, or a boolean). The result of an expression evaluated as part of an if statement is read as either true or false. The expression of a switch statement yields a result that is tested against any number of values. switch ( expression ) { case result1: // execute this if expression results in result1 case result2: break; default: No The switch statement's expression is often simply a variable. Within the switch tests a value against the result of the switch statement's expression. If these are statement ends execution of the switch statement altogether. If this is left out, the next case statement's expression is evaluated. If the optional default statement is reached, its code is executed. statement's block of code, you find a number of case statements. Each of these equivalent, then the code after the case statement is executed. The break tf or } co // execute this if no break statement // has been encountered hitherto m er // execute this if expression results in result2 cia l, break; http://i-tronics.blogspot.com/ us e fo re du ca tio evaluate multiple expressions. switch evaluates only one expression, executing n and if statements. Using the if statement in conjunction with elseif, you may on ly 76 Caution Don't forget to include a break statement at the end of any code that will be executed as part of a case statement. Without break, the program flow will continue to the next case statement and ultimately to the default statement. In most cases, this will not be the behavior that you will be expecting. statement. Listing 5.4: A switch Statement 1: 2: 3: Listing 5.4 4: 5: 6: 20: 21: Once again, the $mood variable is initialized to "sad". The switch statement uses this variable as its expression. The first case statement tests for equivalence http://i-tronics.blogspot.com/ tf or } co break; print "Neither happy nor sad but $mood"; m er cia l, us e fo re du ca tio n on ly Listing 5.4 re-creates the functionality of the if statement example, using the switch 77 between "happy" and the value of $mood. There is no match, so script execution moves on to the second case statement. The string "sad" is equivalent to the value of $mood, so this block of code is executed. The break statement ends the process. Using the ? Operator The ? or ternary operator is similar to the if statement but returns a value derived from one of two expressions separated by a colon. Which expression is used to generate the value returned depends on the result of a test expression: If the test expression evaluates to true, the result of the second expression is returned; otherwise, the value of the third expression is returned. Listing 5.5 uses the ternary operator to set the value of a variable according to the value of $mood. Listing 5.5: Using the ? Operator 1: 2: 3: Listing 5.5 4: 5: 6: 11: No 12: $mood is set to "sad". $mood is tested for equivalence to the string "happy". Because this test returns false, the result of the third of the three expressions is returned. tf or co $mood"; m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca _false; tio ( expression )?returned_if_expression_is_true:returned_if_expression_is n on ly 78 The ternary operator can be difficult to read but is useful if you are dealing with only two alternatives and like to write compact code. Loops Scripts can also decide how many times to execute a block of code. Loop statements are designed to enable you to achieve repetitive tasks. Almost without exception, a exit the loop. The while Statement loop continues to operate until a condition is achieved, or you explicitly choose to The while statement looks similar in structure to a basic if statement: while ( expression ) { // do something } executed over and over again. Within the block, you usually change something that affects the while statement's expression; otherwise, your loop continues indefinitely. Listing 5.6: A while Statement 1: 2: Listing 5.6 creates a while loop that calculates and prints multiples of two. No 10: tf 4: 5: 6: "; or 3: Listing 5.6 co m er cia l, As long as a while statement's expression evaluates to true, the code block is http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly So far you've looked at decisions that a script can make about what code to execute. 79 11: 12: 13: ?> 14: 15: $counter++; } In this example, we initialize a variable $counter. The while statement tests the value contained by $counter is multiplied by two, and the result is printed to the browser. Then $counter is incremented. This last stage is extremely important. If you were to forget to change $counter, the while expression would never resolve to false, and the loop would never end. The do..while Statement truth test and not after it: do { // code to be executed } while ( expression ); semicolon. This statement might be useful if you want the code block to be executed at least statement. The code block is executed a minimum of one time. Listing 5.7: The do...while Statement 1: 2: 3: Listing 5.7 4: No tf once even if the while expression evaluates to false. Listing 5.7 creates a do...while or co Note The test expression of a do...while statement should always end with a m er cia l, http://i-tronics.blogspot.com/ us essential difference between the two is that the code block is executed before the e A do...while statement looks a little like a while statement turned on its head. The fo re du ca tio equal to 12, the loop continues to run. Within the while statement's code block, the n $counter variable. As long as the integer contained by $counter is smaller than or on ly 80 5: 6: 15: 16: { print "Execution number: $num
\n"; $num++; } while ( $num > 200 && $num < 400 ); The do...while statement tests whether the variable $num contains a value that is greater than 200 and smaller than 400. We have initialized $num to 1 so this expression returns false. Nonetheless, the code block is executed before the expression is evaluated, so the statement will print a single line to the browser. The for Statement You cannot achieve anything with a for statement that you cannot do with a while statement. On the other hand, the for statement is often a neater and safer way of statement. The while statement then tested the variable in its expression. The variable was incremented within the code block. The for statement allows you to achieve this on a single line. This allows for more compact code and makes it less loop. likely that you forget to increment a counter variable, thereby creating an infinite achieving the same effect. Earlier, Listing 5.6 initialized a variable outside the while No for ( variable assignment; test expression; variable increment ) { // code to be executed } tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 81 Each of the expressions within the parentheses of the for statement is separated by semicolons. Usually, the first expression initializes a counter variable, the second expression is the test condition for the loop, and the third expression increments the counter. Listing 5.8 shows a for statement that re-creates the example in Listing 5.6, which multiplies 12 numbers by 2. Listing 5.8: Using the for Statement 1: 2: 3: Listing 5.8 4: 5: 6: 12: 13: { print "$counter times 2 is ".($counter*2)."
"; } makes the code more compact. Because $counter is initialized and incremented at the top of the statement, the logic of the loop is clear at a glance. Within the for statement's parentheses, the first expression initializes the $counter variable and sets it to 1. The test expression checks that $counter contains a value that is less than or equal to 12. The final expression increments the $counter variable. When program flow reaches the for loop, the $counter variable is initialized, and the No test expression is evaluated. If the expression evaluates to true, the code block is executed. The $counter variable is then incremented and the test expression evaluated again. This process continues until the test expression evaluates to false. tf or co m er The results of Listings 5.6 and 5.8 are exactly the same. The for statement, though, cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 82 Breaking Out of Loops with the break Statement Both while and for statements incorporate a built-in test expression with which you can end a loop. The break statement, though, enables you to break out of a loop creates a simple for statement that divides a large number by a variable that is incremented, printing the result to the screen. Listing 5.9: A for Loop That Divides 4000 by Ten Incremental Numbers according to additional tests. This can provide a safeguard against error. Listing 5.9 3: Listing 5.9 4: 5: 6: $temp = 4000/$counter; } 13: 14: This example initializes the variable $counter to 1. The for statement's test block, 4000 is divided by $counter, printing the result to the browser. This seems straightforward enough. What, though, if the value you place in $counter comes from user input? The value could be a minus number, or even a string. Let's take the first instance. Changing the initial value of $counter from 1 to − 4 causes 4000 to be divided by zero as the code block is executed for the fifth time, which is not advisable. Listing 5.10 guards against this by breaking out of the loop if the $counter variable contains zero. No tf expression checks that $counter is smaller than or equal to 10. Within the code or co m er cia l, print "4000 divided by $counter is... $temp
"; http://i-tronics.blogspot.com/ us 8: { e fo re du ca 2: tio 1: n on ly 83 Listing 5.10: Using the break Statement 1: 2: 3: Listing 5.10 4: 5: 6: { if ( $counter == 0 ) break; $temp = 4000/$counter; print "4000 divided by $counter is... $temp
"; 16: 17: Note Dividing a number by zero does not cause a fatal error in PHP4. Instead, a warning is generated and execution continues. Use an if statement to test the value of $counter. If it is equivalent to zero, the break continues after the while statement. Notice that we initialized the $counter variable outside the for statement's parentheses to simulate a situation in which the value of $counter is set according to form input or a database look up. Tip You can omit any of the expressions of a for statement, but you must remember to retain the semicolons. No tf or statement immediately halts execution of the code block, and program flow co m er cia l, http://i-tronics.blogspot.com/ us } e fo re du ca 8: for ( ; $counter <= 10; $counter++ ) tio n on ly 84 Skipping an Iteration with the continue Statement The continue statement ends execution of the current iteration but doesn't cause the loop as a whole to end. Instead, the next iteration is immediately begun. Using in Listing 5.11, you can avoid a divide by zero error without ending the loop completely. Listing 5.11: Using the continue Statement 1: 2: 3: Listing 5.11 the break statement in Listing 5.10 was a little drastic. With the continue statement 4: 5: 6: if ( $counter == 0 ) continue; $temp = 4000/$counter; } No 17: We have swapped the break statement for a continue statement. If the $counter variable is equivalent to zero, the iteration is skipped, and the next one immediately is started. Caution The break and continue statements can make code more difficult tf 16: or co print "4000 divided by $counter is... $temp
"; m er cia l, 9: { http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 85 to read. Because they often add layers of complexity to the logic of the loop statements that contain them, they can lead to obscure bugs. They are best used sparingly. Nesting Loops Loop statements can contain other loop statements. This combination is particularly for statements to print a multiplication table to the browser. Listing 5.12: Nesting Two for Loops 1: 2: 3: Listing 5.12 4: 5: 6: \n"; 8: for ( $y=1; $y<=12; $y++ ) 9: 10: { useful when working with dynamically created HTML tables. Listing 5.12 uses two print "\n"; 11: 12: 13: 14: 15: 16: for ( $x=1; $x<=12; $x++ ) { tf No 17: 18: 19: print ""; 20: ?> 21: or print "\n"; } co print "\t"; print ($x *$y); print "\n"; } m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 86 22: The outer for statement initializes a variable called $y, setting its starting value to 1. It defines an expression that tests that $y is smaller or equal to 12 and defines the increment for $y. For each iteration, the code block prints a TR (table row) HTML called $x and defines expressions along the same lines as for the outer loop. For each iteration, the inner loop prints a TD (table cell) element to the browser, as well table. as the result of $x multiplied by $y. The result is a neatly formatted multiplication element and defines another for statement. This inner loop initializes a variable Summary In this hour, you learned about control structures and the ways in which they can help to make your scripts flexible and dynamic. Most of these structures will reappear regularly throughout the rest of the book. change flow according to multiple equivalence tests on the result of an expression. learned how to use break and continue to prematurely end the execution of a loop or to skip an iteration. Finally, you learned how to nest one loop within another and saw a typical use for this structure. No or an empty string will be converted to false for the purposes of the test. All other values will evaluate to true. Q Must I always surround a code block in a control statement with brackets? A If the code you want executed as part of a control structure consists of only a single line, you can omit the brackets. tf A Ultimately, yes, but in the context of a test expression zero, an undefined variable, or Q Must a control structure's test expression result in a boolean value? co Q&A m er cia l, You learned about loops, in particular, the while and for statements, and you http://i-tronics.blogspot.com/ us with the elseif and else clauses. You learned how to use the switch statement to e You learned how to define an if statement and how to provide for alternative actions fo re du ca tio n on ly 87 Q Does this hour cover every kind of loop there is? A In Hour 7, "Arrays," you encounter the foreach statement, which enables you to loop through every element in an array. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 88 Hour 6: Functions Overview Functions are the heart of a well-organized script, making code easy to read and reuse. No large project would be manageable without them. ways in which they can save you from repetitive work. In this hour, you will learn How to pass values to functions and receive values in return How to access global variables from within a function How to give a function a "memory" How to pass data to functions by reference How to call a function dynamically using a string stored in a variable feed it and works with them to achieve a purpose or to produce a product. A function the browser, for example) or returns a new value, possibly both. If you needed to bake a single cake, you would probably do it yourself. If you cake-baking machine. Similarly, when deciding whether to create a function, the most important factor to consider is the extent to which it can save you from repetition. A function, then, is a self-contained block of code that can be called by your scripts. When called, the function's code is executed. You can pass values to functions, the calling code. NEW TERM A function is a block of code that is not immediately executed but can be called by your scripts when needed. Functions can be built-in or user-defined. They can require information to be passed to them and usually return a value. which they will then work with. When finished, a function can pass a value back to needed to bake thousands of cakes, you would probably build or acquire a No Calling Functions Functions come in two flavors— those built in to the language and those you define http://i-tronics.blogspot.com/ tf or co m er cia l, accepts values from you, processes them, and then performs an action (printing to us You can think of a function as a machine. A machine takes the raw materials you e What Is a Function? fo re du ca How to define and call functions tio n Throughout this hour, we will investigate functions and demonstrate some of the on ly 89 yourself. PHP4 has hundreds of built-in functions. The very first script in this book consisted of a single function call: print("Hello Web"); Note print() is not a typical function in that it does not require parentheses in order to run successfully. print(("Hello Web"); and print "Hello Web"; parentheses, whether or not they accept arguments. are equally valid. This is an exception. All other functions require In this example, we called the print() function, passing it the string "Hello Web". The of the function name, print in this case, followed by parentheses. If you want to pass information to the function, you place it between these parentheses. A piec e of information passed to a function in this way is called an argument. Some functions require that more than one argument be passed to them. Arguments in these cases must be separated by commas: some_function( $an_argument, $another_argument ); successful. print() returns a boolean, therefore. absolute value of that number. Let's try it out in Listing 6.1. Listing 6.1: Calling the Built in abs() Function 2: 4: 5: 6: 3: Listing 6.1 No 10: // prints "321" 11: ?> 12: 13: tf 7: $num = − 321; 8: $newnum = abs( $num ); 9: print $newnum; or co m er cia l, The abs() function, for example, requires a signed numeric value and returns the http://i-tronics.blogspot.com/ us back when they've completed their task, if only to tell whether their mission was e print() is typical in that it returns a value. Most functions give you some information fo re du ca function then went about the business of writing the string. A function call consists tio n on ly 90 In this example, we assign the value − 321 to a variable $num. We then pass that variable to the abs() function, which made the necessary calculation and returned a new value. We assign this to the variable $newnum and print the result. In fact, we could have dispensed with temporary variables altogether, passing our number straight to abs(), and directly printing the result: print( abs( − 321 ) ); The rules for calling user-defined functions are almost exactly the same. NEW TERM An argument is a value passed to a function. Arguments are included within the parentheses of a function call. User-defined functions of the function definition. These arguments then become available to Defining a Function You can define a function using the function statement: function some_function( $argument1, $argument2 ) { // function code here } parentheses. If your function is us The name of the function follows the function statement and precedes a set of to require arguments, you must place comma-separated variable names within the parentheses. These variables will be filled by the values passed to your function. If your function requires no arguments, you must nevertheless supply the parentheses. Listing 6.2: Declaring a Function 1: 2: Listing 6.2 declares a function. 3: Listing 6.2 4: 5: 6: No 11: bighello(); tf 7: function bighello() { print "

HELLO!

"; } or co m er cia l, http://i-tronics.blogspot.com/ e fo re du ca the function as local variables. tio n include comma-separated argument names within the parentheses on ly 91 13: 14: The script in Listing 6.2 will simply output the string "HELLO" wrapped in an HTML

element. We declare a function bighello() that requires no arguments. Because of this, we leave the parentheses empty. bighello() is a working function but not terribly useful. Listing 6.3 creates a function that requires an argument and actually does something helpful with it. Listing 6.3: Declaring a Function That Requires Arguments 1: 2: 3: Listing 6.3 4: 5: 6: \n"); } 12: printBR("This is a new line"); 14: ?> 15: 16: No Figure 6.1: A function that prints a string with an appended
tag. tf or co m er cia l, 13: printBR("This is yet another line"); http://i-tronics.blogspot.com/ us 11: printBR("This is a line"); e fo re du ca tio n on ly 92 You can see the output from the script in Listing 6.3 in Figure 6.1. The printBR() function expects a string, so we place the variable name $txt between the parentheses when we declare the function. Whatever is passed to printBR() is stored in $txt. Within the body of the function, we print the $txt variable, appending a
element and a newline character to it. Now when we want to write a line to the browser, we can call printBR() instead of the built-in print(), saving us the bother of typing the
element. Returning Values from User-Defined Functions or object. return stops the execution of the function and sends the value back to the Listing 6.4 creates a function that returns the sum of two numbers. Listing 6.4: A Function That Returns a Value 1: 2: 3: Listing 6.4 4: 5: 6: 15: 16: No two numeric arguments (3 and 5 in this case). These are stored in the variables $firstnum and $secondnum. Predictably, addNums() adds the numbers contained in these variables together and stores the result in a variable called $result. Once again, we can dispense with a stage in this code, doing away with the temporary $result variable altogether: function addNums( $firstnum, $secondnum ) { tf The script in Listing 6.4 will print the number '8'. addNums() should be called with or co m er 12: print addNums(3,5); cia l, http://i-tronics.blogspot.com/ us e fo re du ca calling code. tio n A function can return a value using the return statement in conjunction with a value on ly 93 return ( $firstnum + $secondnum ); } The return statement can return a value, an object, or even nothing at all. How a value passed by return is arrived at can vary. The value could be hard-coded: return 4; It could be the result of an expression: return ( $a/$b ); It could be the value returned by yet another function call: return ( another_function( $an_argument ) ); variables exactly as you would the function name itself. Listing 6.5 creates a simple example of this. 1: 2: 3: Listing 6.5 4: 5: 6: "; 11: $function_holder = "sayHello"; 12: $function_holder(); 13: ?> 14: 15: No A string identical to the name of the sayHello() function is assigned to the $function_holder variable. Once this is done, we can use this variable in conjunction Why would we want to do this? In the example, we simply made more work for ourselves by assigning the string "sayHello" to $function_holder. Dynamic function calls are useful when you want to alter program flow according to changing circumstances. We might want our script to behave differently according to a with parentheses to call the sayHello() function. tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca It is possible to assign function names as strings to variables and then treat these tio Dynamic Function Calls n on ly 94 parameter set in a URL's query string, for example. We could extract the value of this parameter and use it to call one of a number of functions. PHP's built-in functions also make use of this feature. The array_walk() function, for example uses a string to call a function for every element in an array. You can see an example of array walk() in action in Hour 16. Variable Scope projects, this can save you from accidentally overwriting the contents of a variable when you declare two variables of the same name in separate functions. Listing 6.6 creates a variable within a function and then attempts to print it outside the function. Unavailable Outside the Function 1: 2: 3: Listing 6.6 4: 5: 6: 13: 14: tf 11: print "test variable: $testvariable
"; or } co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio it will not be available outside the function or within other functions. In larger n A variable declared within a function remains local to that function. In other words, on ly 95 You can see the output of the script in Listing 6.6 in Figure 6.2. The value of the variable $testvariable is not printed. This is because no such variable exists outside the test() function. Note that attempting to access a nonexistent variable does not cause an error. Similarly, a variable declared outside a function will not automatically be available Accessing Variables with the global Statement From within a function, it is not possible by default to access a variable that has set or access a local variable only. Let's put this to the test in Listing 6.7. Listing 6.7: Variables Defined Outside Functions Are Inaccessible from Within a Function by Default 1: 2: No tf 3: Listing 6.7 4: 5: 6: "; } 12: meaningOfLife(); 13: ?> 14: 15: You can see the output from the script in Listing 6.7 in Figure 6.3. As you might empty when the function attempts to print it. On the whole, this is a good thing. We're saved from potential clashes between identically named variables, and a function can always demand an argument if it needs information about the outside world. Occasionally, however, you may want to access an important global variable from within a function without passing it in as an argument. This is where the global statement comes into its own. Listing 6.8 uses global to restore order to the universe. No Listing 6.8: Accessing Global Variables with the global Statement 1: 2: 3: Listing 6.8 4: 5: tf Figure 6.3: Attempting to print a global variable from within a function. or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca expect, the meaningOfLife() function has no access to the $life variable; $life is tio n on ly 97 6: "; } 14: ?> 15: 16: You can see the output from the script in Listing 6.8 in Figure 6.4. By placing global in front of the $life variable when we declare it in the meaning_of_life() function, we make it refer to the global $life variable declared outside the function. particular global variable. Be careful, though. If we manipulate the contents of the variable within the function, $life will be changed for the script as a whole. Usually, an argument is a copy of beyond the function block. Changing a global variable within a function on the other hand changes the original and not a copy. Use the global statement sparingly. whatever value is passed by the calling code; changing it in a function has no effect No Figure 6.4: Successfully accessing a global variable from within a function using tf or co m er cia l, http://i-tronics.blogspot.com/ us You will need to use the global statement for every function that wishes to access a e fo re du ca tio n 13: meaningOfLife(); on ly 98 the global keyword. Saving State Between Function Calls with the static Statement Variables within functions have a short but happy life on the whole. They come into being when the is finished. Once again, this is as it should be. It is usually best to build a script as a series of self-contained blocks, each with as little knowledge of others as possible. Occasionally, however, you may want to give a function a rudimentary memory. been called. Why? In our examples, the function is designed to create numbered We could, of course use our newfound knowledge of the this. We have a crack at this in Listing 6.9. Variable Between Function Calls 1: 2: 3: Listing 6.9 4: 5: 6: $num_of_calls. $txt

"; No 15: print("We build a fine range of widgets

"); 17: print("Finest in the world

"); 18: ?> 19: 20: 16: andAnotherThing("Doodads"); tf 14: andAnotherThing("Widgets"); or } co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca headings in a script that dynamically builds online documentation. global statement to do tio n Let's assume that we want a function to keep track of the number of times it has on ly andAnotherThing() function is called and die when execution 99 function has been called. This does the job. We declare a variable, andAnotherThing(). We make this variable available to the function with the global statement. You can see the output of Listing 6.9 in Figure 6.5. Every time andAnotherThing() is called, $num_of_calls is incremented. We can then print out a heading complete with a heading number. fo re du ca Figure 6.5: Using the global statement to keep track of the number of times a $num_of_calls, outside the function tio we need to look out for the global variables that they manipulate. function. On the other hand, the function "remembers" the value of the variable static statement. Listing 6.10: Using the static Statement to Remember the Value of a Variable Between Function Calls 1: 2: No 10: tf 3: Listing 6.10 4: 5: 6: $num_of_calls. $txt"; } 13: andAnotherThing("Widgets"); 14: print("We build a fine range of widgets

"); 15: andAnotherThing("Doodads"); 16: print("Finest in the world

"); 17: ?> 18: 19: is ignored when the function is called a second time. Instead, the previous value of $num_of_calls is remembered. We can now paste the andAnotherThing() function into other scripts without worrying about global variables. Although the output of Listing 6.10 is exactly the same as that for Listing 6.9, we have made the code more elegant. You've already seen how to pass arguments to functions, but there's more to cover yet. In this section, you'll look at a technique for giving your arguments default values and explore a method of passing references to variables rather than copies of their values to your functions. Setting Default Values for Arguments PHP gives you a nifty feature to help build flexible functions. Until now, we've said that some functions "demand" one or more arguments. By making some arguments No Listing 6.11 creates a useful little function that wraps a string in an HTML font element. We want to give the user of the function the chance to change the font element's size attribute, so we demand a $size argument in addition to the string. Listing 6.11: A Function Requiring Two Arguments 1: 2: tf optional, you can render your functions a little less autocratic. or co m er cia l, http://i-tronics.blogspot.com/ us More About Arguments e fo re du ca andAnotherThing() has become entirely self-contained. When we declare the $num_of_calls variable, we assign an initial value to it. This initial assignment tio n on ly 101 3: Listing 6.11 4: 5: 6: $txt";10: } 11: fontWrap("A heading
",5); 12: fontWrap("some body text
",3); 13: fontWrap("some more body text
",3); 14: fontWrap("yet more body text
",3); 15: ?> 16: 17: Figure 6.6: A function that formats and outputs strings. No You can see the output from the script in Listing 6.11 in Figure 6.6. Useful though this function is, we really only need to change the font size occasionally. Most of the time we default to 3. By assigning a value to an argument variable within the function definition's parentheses, we can make the $size argument optional. If the function call doesn't define an argument for this, the value we have assigned to the tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 102 argument is used instead. Listing 6.12 uses this technique to make the $size argument optional. Listing 6.12: A Function with an Optional Argument 1: 2: 3: Listing 6.12 4: 5: 6: $txt"; 10: } 11: fontWrap("A heading
",5); 12: fontWrap("some body text
"); 13: fontWrap("some more body text
"); 14: fontWrap("yet more body text
"); 16: 17: set the size attribute of the font element. When we omit this argument, the default value of 3 is used instead. You can create as many optional arguments as you want, but when you've given an argument a default value, all subsequent arguments should also be given defaults. No Passing References to Variables to Functions When you pass arguments to functions they are stored as copies in parameter variables. Any changes made to these variables in the body of the function are local to that function and are not reflected beyond it. This is illustrated in Listing 6.13. Listing 6.13: Passing an Argument to a Function by Value tf or co When the fontWrap() function is called with a second argument, this value is used to m er cia l, 15: ?> http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 103 1: 2: 3: Listing 6.13 4: 5: 6: 15: 16: The addFive() function accepts a single numeric value and adds 5 to it. It returns nothing. We assign a value to a variable $orignum and then pass this variable to Although we increment $num by 5, this has no effect on the value of $orignum. When we print $orignum, we find that its value is still 10. By default, variables of the variables are made. It is possible to pass arguments to functions by reference. This means that a variable's value. Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition. passed to functions are passed by value. In other words, local copies of the values addFive(). A copy of the contents of $orignum is stored in the variable $num. No Listings 6.14 and 6.15 show each technique in turn. Listing 6.14: Using a Function Call to Pass an Argument to a Function by Reference 1: tf reference to the variable is manipulated by the function rather than a copy of the or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 104 2: 3: Listing 6.14 4: 5: 6: 15: 16: Listing 6.15: Using a Function Definition to Pass an Argument to a Function 1: 2: 3: Listing 6.15 4: 5: 6: 15: 16: In this way, you can be sure that the function behaves consistently from call to call. In this hour, you learned about functions and how to deploy them. You learned how to define and pass arguments to a function. You learned how to use the global and static statements. You learned how to pass references to functions and how to create default values for function arguments. Q&A associative array called $GLOBALS. To access a global variable called $test within a function, you could reference it as $GLOBALS[test]. You can learn more about associative arrays in the next hour. You can also change global variables from within a function if it has been passed in Q Can you include a function call within a string, as you can with a variable? A No. You must call functions outside quotation marks. No Workshop The Workshop provides quiz questions to help you solidify your understanding of the material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. tf or co by reference. m er cia l, A You can also access global variables anywhere in your scripts with a built-in http://i-tronics.blogspot.com/ us access and change global variables? e Q Apart from the global keyword, is there any way that a function can fo re du ca tio n Summary on ly On the whole, it makes more sense to add the ampersand to the function definition. 106 Quiz True or False: If a function doesn't require an argument, you can omit the parentheses in the function call. How do you return a value from a function? What would the following code fragment print to the browser? $number = 50; function tenTimes() { $number = $number } tenTimes(); print $number; * What would the following code fragment print to the browser? $number = 50; function tenTimes() { global $number; $number = $number tenTimes(); print $number; What would the following code fragment print to the browser? No tf function tenTimes( $n ) { or $number = 50; tenTimes( $number ); print $number; co $n = $n } m er } * 10; cia l, http://i-tronics.blogspot.com/ us * 10; e fo re du ca 10; tio n on ly 107 What would the following code fragment print to the browser? $number = 50; function tenTimes( &$n ) { $n = $n } tenTimes( $number ); print $number; Activity * 10; Create a function that accepts four string variables and returns a string that contains an HTML table element, enclosing each of the variables in its own cell. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 108 Hour 7: Arrays Overview Arrays, and the tools to manipulate them, greatly enhance the scope and flexibility of PHP4 scripts. After you've mastered arrays, you will be able to store and organize complex data structures. This hour introduces arrays and some of the functions that help you work with them. What arrays are and how to create them How to access data from and about arrays How to access and sort the data contained in arrays What Is an Array? You already know that a variable is a "bucket" in which you can temporarily store a outputs different information every time it is run. Unfortunately, you can only store one value at a time in a variable. Arrays are special variables that allow you to in the same variable. Each value is indexed within the array by a number or a string. If a variable is a bucket, you can think of an array as a filing cabinet— a single NEW TERM container that can store many discrete items. overcome this limitation. An array allows you to store as many values as you want Of course, if you have five values to store, you could always define five variables. So, why use an array rather than a variable? First, an array is flexible. It can store two an array allows you to work easily with all its items. You can loop through each item or pull one out at random. You can sort items numerically, alphabetically, or even according to a system of your own. Each item in an array is commonly referred to as an element. Each element can be accessed directly via its index. An index to an array element can be either a number or a string. By default, array elements are indexed by number starting at zero. It's important to remember, therefore, that the index of the last element of a numerically indexed http://i-tronics.blogspot.com/ No tf values or two hundred values without the need to define further variables. Second, or co m er An array is a list variable. That is, a variable that contains multiple elements indexed by numbers or strings. It enables you to store, order, and access many values under one name. cia l, us e value. By using variables, you can create a script that stores, processes, and fo re du ca In this hour, you will learn tio n on ly 109 array is always one less than the number of elements the array contains. For example, Table 7.1 shows the elements in an array called users. Notice that the third element has an index of 2. Table 7.1: The Elements in the users Array Index Number 0 1 2 3 Bert Sharon Betty Harry Value Which First Second Third Fourth and values. PHP4 provides tools to access and manipulate arrays indexed by both name and number. Some of these are covered in this hour, and others will be covered in Hour 16, "Working with Data." By default, arrays are lists of values indexed by number. Values can be assigned to an array in two ways: with the array() function or directly using the array identifier []. You'll meet both of these in the next two sections. Defining Arrays with the array() Function one time. Let's define an array called $users and assign four strings to it: No print "$users[2]"; This would return the string "Sharon". The index of an array element is placed between square brackets directly after the array name. You can use this notation either to set or retrieve a value. Remember that arrays are indexed from zero by default, so the index of any element always is one less than the element's place in the list. tf You can now access the third element in the $user array by using the index "2": or $users = array ("Bert", "Sharon", "Betty", "Harry" ); co The array() function is useful when you want to assign multiple values to an array at m er cia l, http://i-tronics.blogspot.com/ us e Creating Arrays fo re du ca Indexing arrays by string can be useful in cases where you need to store both names tio n on ly Element? 110 Defining or Adding to Arrays with the Array Identifier You can create a new array (or add to an existing one) by using the array identifier in conjunction with the array name. The array identifier is a set of square brackets with no index number or name inside it. Let's re-create our $users array in this way: $users[ ] = " Bert"; $users[ ] = " Sharon"; $users[ ] = " Betty"; $users[ ] = " Harry"; Notice that we didn't need to place any numbers between the square brackets. PHP4 automatically takes care of the index number, which saves you from havi ng to work out which is the next available slot. We could have added numbers if we wanted, and the result would have been exactly the same. It's not advisable to do this, though. Take a look at the following code: $users[0] = " Bert"; $users[200] = "Sharon"; The array has only two elements, but the index of the final element is 200. PHP4 will to access elements in the array. In addition to creating arrays, you can use the array identifier to add new values onto the end of an existing array. In the following code, we define an array with the array() function and use the array identifier to add a new element: $users = array ("Bert", " Sharon", "Betty", "Harry" ); $users[] = "sally"; No Associative Arrays Numerically indexed arrays are useful for storing values in the order in which they were added or according to a sort pattern. Sometimes, though, you need to access elements in an array by name. An associative array is indexed with strings between tf or co m er not initialize the intervening elements. This could lead to confusion when attempting cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 111 the square brackets rather than numbers. Imagine an address book. Which would be easier, indexing the "name" field as 4 or as "name"? NEW TERM Arrays indexed by strings are known as associative arrays. You may also see them referred to as hashes. Once again, you can define an associative array using either array() or the array Tip The division between an associative array and a numerically indexed hashes are in Perl. It is a good idea, nevertheless, to treat them manipulation. Defining Associative Arrays with the array() Function array is not absolute in PHP. They are not separate types as arrays and separately. Each demands different strategies for n access To define an associative array with the array() function, you must define both the key and value for each element. The following code creates an associative array called $character with four elements: $character = array ( name=>"bob", age=>30, print $character[age]; The keys in an associative array are strings, but it isn't necessary to surround them with quotation marks unless the key contains more than one word. No Directly Defining or Adding to an Associative Array You can create or add a name/value pair to an associative array simply by assigning a value to a named element. In the following, we re-create our $character array by directly assigning a value to each key: tf or co We can now access any of the fields of $character: m er "special power"=>"x-ray vision" ); cia l, occupation=>"superhero", http://i-tronics.blogspot.com/ us e fo re du ca tio on ly and identifier []. 112 $character[name] = "bob"; $character[occupation] = "superhero"; $character[age] = 30; $character["special power"] = "x-ray vision"; Until now, we've simply said that elements of arrays are values. In our $character array, three of the elements held strings, and one held an integer. The reality is a little more complex, however. In fact, an element of an array could be a value, an Imagine an array that stores an array in each of its elements. To access the third element of the second element, you would have to use two indices: $array[1][2] NEW TERM multidimensional arrays. Arrays that contain arrays as their elements are known as The fact that an array element can itself be an array enables you to create sophisticated data structures relatively easily. 7.1 defines an array that has an Listing 7.1: Defining a Multidimensional Array 1: 2: 4: 5: 6: Listing 7.1 associative array as each of its elements. 7: $characters = array ( or tf No co m er array ( name=>"bob", occupation=>"superhero", age=>30, specialty=>"x-ray vision" ), array ( name=>"sally", occupation=>"superhero", age=>24, specialty=>"superhuman strength" ), array ( name=>"mary", occupation=>"arch villain", age=>63, specialty=>"nanotechnology" ) http://i-tronics.blogspot.com/ cia l, us e fo re du ca tio object, or even another array. A multidimensional array is an array of arrays. n on ly Multidimensional Arrays 113 20: 21: 22: print $characters[0][occupation]; 23: // prints "superhero" 24: ?> 25: 26: ); Notice that we have nested array function calls within an array function call. At the array. top-level array. We can then go ahead and access any of the associative array's fields. $user[2][name] will be "mary", and $user[2][age] will be 63. When this concept is clear, it will be easy to create complex combinations of associative and numerically indexed arrays. So far, you've seen the ways in which you can create and add to arrays. In this section, you will examine some of the tools that PHP4 provides to allow you to acquire information about arrays and access their elements. Getting the Size of an Array print $user[4] Because of the flexibility of arrays, however, you won't always know how many play. count() returns the number of elements in an array. In the following code, we define a numerically indexed array and use count() to access its last element: $users = array ("Bert", "Sharon", "Betty", "Harry" ); No print $users[count($users)− 1]; Notice that we subtract 1 from the value returned by count(). This is because count() returns the number of elements in an array, not the index of the last element. tf elements a particular array contains. That's where the count() function comes into or co You can access an element of an array by using its index: m er cia l, http://i-tronics.blogspot.com/ us e Accessing Arrays fo re du ca Accessing $user[2], therefore, gives us access to the third associative array in the tio n first level, we define an array. For each of its elements, we define an associative on ly 114 Although arrays are indexed from zero by default, it is possible to change this. For the sake of clarity and consistency, however, this is not usually advisable. Looping Through an Array examples, you'll use PHP4's powerful foreach statement. You will examine some other methods inHour 16. Note The foreach statement was introduced with PHP4. In the context of numerically indexed arrays, you would use a foreach statement foreach( $array as $temp ) { //... } In the following code, we define a numerically indexed array and use foreach to access each of its elements in turn: $users = array ("Bert", "Sharon", "Betty", "Harry" ); { } You can see the output from this code fragment in Figure 7.1. No tf or co print "$val
"; m er foreach ( $users as $val ) cia l, http://i-tronics.blogspot.com/ us you will temporarily store each element. e where $array is the array you want to loop through, and $temp is a variable in which fo re du ca like this: tio n on ly There are many ways of looping through each element of an array. For these 115 Figure 7.1: Looping through an array. The value of each element is temporarily placed in the variable $val, which we then print to the browser. If you are moving to PHP4 from Perl, be aware of a significant difference in the behavior of foreach. Changing the value of the temporary variable in a Perl foreach loop changes the corresponding element in the array. Changing the temporary variable in the preceding example would have no effect on the $users array. You will look at a way of using foreach to change the values of a numerically indexed array inHour 16 Looping Through an Associative Array To access both the keys and values of an associative array, you need to alter the use of foreach slightly. In the context of associative arrays, you would use a foreach statement like this: foreach( $array as $key=>$value ) { //... No where $array is the array we are looping through, $key is a variable that temporarily holds each key, and $value is a variable that temporarily holds each value. Listing 7.2 creates an associative array and accesses each key and value in turn. Listing 7.2: Looping Through an Associative Array with foreach 1: 2: tf or } co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 116 3: Listing 7.2 4: 5: 6: "bob", occupation=>"superhero", age=>30, "special power"=>"x-ray vision" ); 13: foreach ( $character as $key=>$val ) 14: 15: 16: 17: 18: ?> 19: 20: { print "$key = $val
"; } You can see the output from Listing 7.2 in Figure 7.2. Outputting a Multidimensional Array through each of its elements. No tf created in Listing 7.1. Listing 7.3 defines a similar array and uses foreach to loop or You can now combine these techniques to output the multidimensional array co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 117 Figure 7.2: Looping through an associative array. 1: 2: 3: Listing 7.3 4: 5: 6: "superhero", or tf No 17: 18: 19: 20: 21: co m er age=>30, specialty=>"x-ray vision" ), array ( name=>"sally", occupation=>"superhero", age=>24, specialty=>"superhuman strength" ), array ( name=>"mary", occupation=>"arch villain", age=>63, specialty=>"nanotechnology" ) ); cia l, array ( name=>"bob", http://i-tronics.blogspot.com/ us e fo re du ca Listing 7.3: Looping Through a Multidimensional Array tio n on ly 118 22: foreach ( $characters as $val ) 23: 24: 25: 26: 27: 28: 29: 30: ?> 31: 32: { foreach ( $val as $key=>$final_val ) { print "$key: $final_val
"; } print "
"; } You can see the output from Listing 7.3 in Figure 7.3. We create two foreach loops. The outer loop accesses each element in the numerically indexed array $users, placing each one in $val. Because $val itself then contains an associative array, we can loop through this, outputting each of its elements (temporarily stored in $key Figure 7.3: Looping through a multidimensional array. No For this technique to work as expected, we need to make sure in advance that $val will always contain an array. To make this code a little more robust, we could use the function is_array() to test $val. is_array() accepts a variable, returning true if the variable is an array, or false otherwise. Manipulating Arrays http://i-tronics.blogspot.com/ tf or co m er cia l, us and $final_val) to the browser. e fo re du ca tio n on ly 119 You can now populate arrays and access their elements, but PHP4 has functions to help you do much more than that with arrays. If you're used to Perl, you'll find some of these eerily familiar! Joining Two Arrays with array_merge() array_merge() accepts two or more arrays and returns a merged array combining second to the first, and loop through the resultant third array: $first = array("a", "b", "c"); $second = array(1,2,3); $third = array_merge( $first, all their elements. In the following example, we create two arrays, joining the $second ); foreach ( $third as $val ) { print "$val
"; } The $third array contains copies of all the elements of both the $first and $second browser with a
tag between each element. Remember that the arrays passed to array_merge() are not themselves transformed. Note The array_merge() function was introduced with PHP4. Adding Multiple Variables to an Array with array_push() array_push() accepts an array and any number of further parameters, each of array_merge() in that the array passed in as the first argument is transformed. array_push() returns the total number of elements in the array. Let's create an array and add some more values to it: No $first = array("a", "b", "c"); $total = array_push( $first, 1, 2, 3 ); print "There are $total elements in \$first

"; tf which is added to the array. Note that the array_push() function is unlike or co m er cia l, arrays. The foreach statement prints this combined array ( 'a', 'b', 'c', 1, 2, 3 ) to the http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 120 foreach ( $first as $val ) { print "$val
"; } transforms, we are able to store this value (6) in a variable and print it to the browser. The $first array now contains its original elements as well the three browser within the foreach statement. you use a dollar sign followed by numbers and letters within a string, PHP will attempt to insert the value of a variable by that name. In the example above we wished to print the string '$first' rather than the value of the $first variable. To print the special character '$', therefore, we must precede it with a backslash. PHP will now print the character instead of interpreting it. This process is often referred to as "escaping" a character. Caution Perl users beware! If you're used to working with Perl's push(), you array_push() it will be added as a single element, creating a array_merge(). argument. In the following example, we use array_shift() in conjunction with a contains elements: No "; tf or while loop. We test the value returned from count() to check whether the array still co array_shift() removes and returns the first element of an array passed to it as an m er Removing the First Element of an Array with array_shift() cia l, multidimensional array. If you want to combine two arrays, use http://i-tronics.blogspot.com/ us should note that if you pass a second array variable to e fo re du ca Notice that we used a backslash character when we printed the string "\$first". If tio n integers we passed to the array_push() function, all of these are printed to the on ly Because array_push() returns the total number of elements in the array it 121 print "there are ".count($an_array)." elements in \$an_array
"; } ?> You can see the output from this fragment of code in Figure 7.4. Figure 7.4: Using array_shift() to remove and print every element in an array. array_shift() is useful when you need to create a queue and act on it until the queue is empty. of PHP4. Note The array_shift() function was added to the language with the advent Slicing Arrays with array_slice() array_slice() allows you to extract a chunk of an array. It accepts an array as an argument, a starting position (offset), and an (optional) length. If the length is starting position onward returned. array_slice() does not alter the array you pass to omitted, array_slice() generously assumes that you want all elements from the it. It returns a new array containing the elements you have requested. No from it: $first = array("a", "b", "c", "d", "e", "f"); $second = array_slice($first, 2, 3); foreach ( $second as $var ) { print "$var
"; tf In the following example, we create an array and extract a new three-element array or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 122 } This will print the elements 'c', 'd', and 'e', separating each by a
tag. Notice that the offset is inclusive if we think of it as the index number of the first element we are requesting. In other words, the first element of the $second array is equivalent to $first[2]. If we pass array_slice() an offset argument that is less than zero, the returned slice will begin that number of elements from the end of the given array. the end of the given array. Note Once again, array_slice() was added to PHP with PHP4. Sorting Arrays Sorting is perhaps the greatest magic you can perform on an array. Thanks to the functions that PHP4 offers to achieve just this, you can truly bring order from chaos. indexed and associative arrays. This section introduces some functions that allow you to sort both numerically Sorting Numerically Indexed Arrays with sort() strings are present or numerically if all elements are numbers. The function doesn't return any data, transforming the array you pass it. Note that it differs from Perl's sort() function in this respect. The following fragment of code initializes an array of single character strings, sorts it, and outputs the transformed array: $an_array = array("x","a","f","c"); sort( $an_array); No foreach ( $an_array as $var ) { tf Caution Don't pass an associative array to sort(). You will find that the values are sorted as expected but that your keys have been or print "$var
"; } co m er sort() accepts an array as its argument and sorts it either alphabetically if any cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio will contain all elements from the offset position to that number of elements from n If we pass array_slice() a length argument that is less than zero, the returned slice on ly 123 lost— replaced by numerical indices that follow the sort order. You can reverse sort a numerically indexed array by using rsort() in exactly the same way as sort(). Sorting an Associative Array by Value with asort() asort() accepts an associative array and sorts its values just as sort() does. However, it preserves the array's keys: $first = array("first"=>5,"second"=>2,"third"=>1); asort( $first ); foreach ( $first as $key => $val ) { print "$key = $val
"; } You can reverse sort an associative array by value with arsort(). No Sorting an Associative Array by Key with ksort() ksort() accepts an associative array and sorts its keys. Once again, the array you pass it will be transformed and nothing will be returned: $first = array("x"=>5,"a"=>2,"f"=>1); tf or Figure 7.5: Sorting an associative array by its values with asort(). co m er cia l, http://i-tronics.blogspot.com/ us You can see the output from this fragment of code in Figure 7.5. e fo re du ca tio n on ly 124 ksort( $first ); foreach ( $first as $key => $val ) { print "$key = $val
"; } You can see the output from this fragment of code in Figure 7.6. You can reverse sort an associative array by key with krsort(). In this hour, you learned about arrays and some of the many tools that PHP4 indexed and associative arrays, and output data from them using a foreach loop. through the information they contain. You learned how to manipulate arrays by that PHP4 makes available to sort arrays. No Q&A Q If the foreach statement was introduced with PHP4, how did programmers using PHP3 iterate through arrays? A The PHP3 technique for looping through an array involved a function called each(), which was used in conjunction with a while statement. You can read about this technique inHour 16. Q Are there any functions for manipulating arrays that we have not tf or adding or removing multiple elements. Finally, you learned some of the techniques co You should be able to combine arrays to create multidimensional arrays and loop m er provides to work with them. You should now be able to create both numerically cia l, Summary http://i-tronics.blogspot.com/ us e Figure 7.6: Sorting an associative array by its keys with ksort(). fo re du ca tio n on ly 125 covered here? A PHP4 supports many array functions. You can read about some more of these in Hour 16 and find them all in the official PHP manual at http://www.php.net/manual/ref.array.php. Q I can discover the number of elements in an array, so should I use a for statement to loop through an array? A You should be cautious of this technique. You cannot be absolutely sure that the array you are reading is indexed by consecutively numbered keys. The Workshop provides quiz questions to help you solidify your understanding of the material covered. Try to understand the quiz answers before continuing to the next hour's lesson. Quiz answers are provided in Appendix A. Quiz What function can you use to define an array? What is the index number of the last element of the array defined below? $users = array("Harry", "Bob", "Sandy"); "Susan" to the $users array defined previously? Which function could you use to add the string "Susan" to the $users array? In PHP4, what is the simplest way of looping through an array? What function would you use to join two arrays? How would you sort an associative array by its keys? How would you find out the number of elements in an array? No tf Create a multidimensional array of movies organized by genre. This should take the form of an associative array with genres as keys ("SF", "Action", "Romance", and so on). Each of this associative array's elements should be an array containing movie names ("2001", "Alien", "Terminator", and so on). Loop through the array you created in Activity 1, outputting each genre and its associated movies to the browser. or Activities co m er cia l, Without using a function, what would be the easiest way of adding the element http://i-tronics.blogspot.com/ us e fo re du ca tio Workshop n on ly 126 Hour 8: Objects Overview Object-oriented programming is dangerous. It changes the way you think about coding, and once the concepts have a hold on you, they don't let go. PHP, like Perl before it, has progressively incorporated more object-oriented aspects into its object-oriented code at the heart of your projects. syntax and structure. With the advent of PHP4, it becomes possible to use Throughout this hour, you'll take a tour of PHP4's object-oriented features and apply them to some real-world code. In this hour, you will learn What objects and classes are How to create classes and instantiate objects How to create and access properties and methods How to create classes that inherit functionality from others your projects An object is an enclosed bundle of variables and functions forged from a special template called a class. Objects hide a lot of their inner workings away from the them orders and they can return information. These interfaces are special functions called methods. All the methods of an object have access to special variables called properties. By defining a class, you lay down a set of characteristics. By creating objects of that type, you create entities that share these characteristics but might initialize them as would have a color characteristic. All automobile objects would share the characteristic of color, but some would initialize it to "blue," others to "green," and so on. A class is a collection of special functions called methods and special variables called properties. You can declare a class with the class keyword. Classes are the templates from which objects are created. NEW Existing in memory rather than as code, an object is an instance of a different values. You might create an automobile class, for example. This class code that uses them, providing instead easy interfaces through which you can send No tf or co m er cia l, What Is an Object? http://i-tronics.blogspot.com/ us e Some of the reasons why object-oriented programming can help you to organize fo re du ca tio n on ly 127 TERM class. That is, an object is the working embodiment of the functionality laid down in a class. An object is instantiated with the new statement in conjunction with the name of the class of which it is to be a member. When an object is instantiated, you can access all its properties and all its methods. Perhaps the greatest benefit of object-oriented code is its reusability. Because the classes used to create objects are self-enclosed, they can be easily pulled from one project and used in another. Additionally, it is possible to create child classes that inherit and override the characteristics of their parents. This technique can allow base functionality while adding more of their own. Creating an Object To create an object, you must first design the template from which it can be instantiated. This template is known as a class, and in PHP4 it must be dec lared with the class keyword: class first_class { // a very minimal class } The first_class class is the basis from which you can instantiate any number of statement: No print "\$obj1 is a ".gettype($obj1)."
"; print "\$obj2 is a ".gettype($obj2)."
"; You can test that $obj1 and $obj2 contain objects with PHP's gettype() function. gettype() accepts any variable and returns a string that should tell you what you are dealing with. In a loosely typed language like PHP, gettype() is useful when tf $obj2 = new first_class(); or $obj1 = new first_class(); co first_class objects. To create an instance of an object, you must use the new m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca Perhaps the best way to explain object-oriented programming is to do it. tio n you to create progressively more complex and specialized objects that can draw on on ly 128 checking arguments sent to functions. In the previous code fragment, gettype() returns the string "object", which is then written to the browser. So, you have confirmed that you have created two objects. Of course they're not very useful yet, but they help to make an important point. You can think of a class as a mold with which you can press as many objects as you want. Let's add some more features to the class to make your objects a little more interesting. Object Properties at the top. A property can be a value, an array, or even another object: class first_class { var $name = "harry"; } Notice that we declared our variable with the var keyword. This is essential in the context of a class, and you will be rewarded with a parse error if you forget it. Now any first_class object that is created will contain a property called name with the value of "harry". You can access this property from outside the object and even change it: class first_class { } var $name = "harry"; $obj1 = new first_class(); $obj2 = new first_class(); $obj1->name = "bob"; print "$obj2->name
"; The -> operator allows you to access or change the properties of an object. No Although $obj1 and $obj2 were born with the name of "harry", we have helped $obj2 to change its mind by assigning the string "bob" to its name property, before using the -> operator once again to print each object's name property to the screen. Caution Object-oriented languages, such as Java, demand that the programmer set a level of privacy for all properties and methods. This means that access can be limited to only those features needed to use the object effectively, and properties meant only for internal use can tf or print "$obj1->name
"; co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio anywhere within the body of your class, but for the sake of clarity should be defined n Objects have access to special variables called properties. These can be declared on ly 129 be safely tucked away. PHP has no such protection. You can access all the fields of an object, which can cause problems if a property isn't meant to be changed. You can use objects to store information, but that makes them little more interesting than associative arrays. In the next section, you will look at object methods, and your objects can get a little more active. Object Methods A method is a function defined within a class. Every object instantiated from the first_class class. Listing 8.1: A Class with a Method 1: 2: 3: Listing 8.1 4: 5: sayHello(); 17: // outputs "hello" 18: ?> 19: No 20: As you can see, a method looks and behaves much like a normal function. A method is always defined within a class, however. You can call an object method using the -> operator. Importantly, methods have access to the class's member variables. You've already seen how to access a property from outside an object, but how does an object refer to itself? Find out in Listing 8.2. tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n class will have the method's functionality. Listing 8.1 adds a method to the on ly 130 Listing 8.2: Accessing a Property from Within a Method 1: 2: 3: Listing 8.2 4: 5: sayHello(); 17: // outputs "hello my name is harry" 18: ?> 19: 20: } { var $name="harry"; function sayHello() { print "hello my name is $this->name
"; } A class uses the special variable $this to refer to the currently instantiated object. You can think of it as a personal pronoun. Although you refer to an object by the means of the $this variable. Combining the $this variable and the -> operator, you can access any property or method in a class from within the class itself. Imagine that you want to assign a different value to the name property to every object of type first_class you create. You could do this by manually resetting the name property as you did earlier, or you could create a method to do it for you, as Listing 8.3: Changing the Value of a Property from Within a Method 1: 2: shown in Listing 8.3. handle you have assigned it to ($obj1, for example), an object must refer to itself by No tf 3: Listing 8.3 4: 5: 6: setName("william"); 23: $obj1->sayHello(); 25: ?> 26: 27: } { var $name="harry"; function setName( $n ) { $this->name = $n; } function sayHello() { print "hello my name is $this->name
"; } 24: // outputs "hello my name is william" The name property of the object begins as "harry", but after the object's setName() method is called, it is changed to "william". Notice how the object is capable of in exactly the same way as you would to a function. We're still missing a trick here, however. If you create a method with exactly the same name as the first_class class, it will automatically be called when a new object is instantiated. In this way, you can give your objects arguments to process at the moment you instantiate them. Objects can run code to initialize themselves based Listing 8.4 adds a constructor to the first_class class. 1: 2: 3: Listing 8.4 4: 5: on these arguments or other factors. These special methods are called constructors. adjusting its own property. Notice also that you can pass arguments to the method No Listing 8.4: A Class with a Constructor tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 132 6: name = $n; } function sayHello() { print "hello my name is $this->name
"; } 19: $obj1 = new first_class("bob"); 20: $obj2 = new first_class("harry"); 21: $obj1->sayHello(); 22: // outputs "hello my name is bob" 23: $obj2->sayHello(); 24: // outputs "hello my name is harry" 25: ?> 26: 27: The first_class() constructor method is automatically called when we instantiate a parameter if we don't include an argument when we create our object. An Example more useful. We will create a class that can maintain a table of fields, organized in No named columns. This data should be built up on a row-by-row basis, and crude a method should be included so that the data can be written to the browser. Neatly formatting the data is not necessary at this stage. tf Let's bring these techniques together to create an example that might be a little or co m er first_class object. We set up a default so that the string "anon" is assigned to the cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 133 Defining the Class's Properties First, we must decide what properties we need to store the data in. We will keep the column names in an array and the rows in a multidimensional array. We'll also store with: class Table { var $table_array = array(); var $headers = array(); var $cols; } Creating a Constructor an integer so that we can easily keep track of the number of columns we're dealing We can do this in the constructor by asking for an array of strin gs as a parameter. result to the cols property: function Table( $headers ) { $this->headers = $headers; $this->cols = count ( $headers ); } created, we will know right away the number of columns we'll be storing and the No name of each column. Because this information has been stored in properties, it will be available to all the object's methods. tf Assuming that the correct information is provided when the new Table object is or co m er cia l, Armed with this information, we can calculate the number of columns and assign the http://i-tronics.blogspot.com/ us We need to get the names of the columns that we'll be working with straight away. e fo re du ca tio n on ly 134 The addRow() Method The Table object accepts each row of data in the form of an array, assuming, of course, that this information is provided in the same order as that of the column names: function addRow( $row ) { if ( count ($row) != $this->cols ) return false; array_push($this->table_array, $row); return true; } The addRow() method expects an array, which is stored in a parameter variable called $row. We have stored the number of columns that the object expects to the right number of elements using the count() function. If it doesn't, a boolean bfalse is returned. handle in the $cols property. We can check that the $row array parameter contains We then use PHP4's array_push() function to add the row array to the table_array property. array_push() accepts two arguments— an array to add to and the value to element of the first array, creating a multidimensional array. In this way, we can build up an array of arrays. push onto it. If the second argument is itself an array, it will be added as a single The addRowAssocArray() Method The addRow() method is fine as long as the elements of the array passed to it are No ordered correctly. The addRowAssocArray() method allows for a little more flexibility. It expects an associative array. The keys for each value should match one of the header names we are storing in our headers property, or they'll be ignored: function addRowAssocArray( $row_assoc ) { $row = array(); tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 135 foreach ( $this->headers as $header ) { if ( ! isset( $row_assoc[$header] )) $row_assoc[$header] = ""; $row[] = $row_assoc[$header]; } array_push($this->table_array, $row); return true; } The associative array passed to addRowAssocArray() is stored in the parameter variable $row_assoc. We create an empty array called $row to store the values that we will eventually add to the table_array property. We loop through the headers array to check that a value corresponding to each string exists in the $row_assoc array. To do this, we use the PHP4 function isset(), which expects any variable as its argument. It returns true if the variable passed to it has been set and false otherwise. We pass isset() the element in the $row_assoc array whose key is the by that string exists in $row_assoc, we go ahead and create one with the value of an element in $row_assoc indexed by the current string in the headers array. By the time we have finished looping through the headers array property, $row contains an of any omissions. ordered copy of the values passed to us in $row_assoc, with empty strings in place current value in the headers property we are looping through. If no element indexed empty string. We can then continue to build up our $row array, adding to it the object's table_array property. All we need now is a way of outputting the data. The output() Method No The output() method writes both the headers and the table_array array properties to the browser. This method is provided mainly for the purpose of debugging. You'll see a more satisfactory solution later in the hour. function output() { print "

"; tf or co We now have two simple methods to allow the addition of rows of data to a Table m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 136 foreach ( $this->headers as $header ) print "$header print "\n"; foreach ( $this->table_array as $y ) { foreach ( $y as $xcell ) print "$xcell print "\n"; } print "
"; } "; "; This code fragment should be fairly self-explanatory. We loop first through the headers array property, writing each element to the screen. We then do the same for the table_array property. Because the table_array property is a two-dimensional array, each of its elements is itself an array that must be looped through within the main loop. Bringing It All Together Listing 8.5 includes the entire Table class, as well the code that instantiates a Table Listing 8.5: The Table Class 2: No 10: tf 4: 5: 6: Listing 8.5 co 1: m er object and calls each of its methods. cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 137 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: function addRow( $row ) { if ( count ($row) != $this->cols ) return false; var $cols; function Table( $headers ) { $this->headers = $headers; $this->cols = count ( $headers ); } array_push($this->table_array, $row); return true; } 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: { $row = array(); foreach ( $this->headers as $header ) or tf No 38: 39: 40: function output() { co array_push($this->table_array, $row); return true; } m er { if ( ! isset( $row_assoc[$header] )) $row_assoc[$header] = ""; $row[] = $row_assoc[$header]; } cia l, http://i-tronics.blogspot.com/ us 26: function addRowAssocArray( $row_assoc ) e fo re du ca tio n on ly 138 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: } print "
"; foreach ( $this->headers as $header ) print "$header "; print "\n"; foreach ( $this->table_array as $y ) { foreach ( $y as $xcell ) print "$xcell "; print "\n"; } print "
"; } 55: $test = new table( array("a","b","c") ); 56: $test->addRow( array(1,2,3) ); 57: $test->addRow( array(4,5,6) ); 58: $test->addRowAssocArray( array ( b=>0, a=>6, c=>3 ) ); 59: $test->output(); 60: ?> 61: 62: You can see the output of Listing 8.5 in Figure 8.1. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 139 Figure 8.1: The Table object in action. The output looks neat as long as the individual strings are the same length. This will change if we vary the length of any of the elements. What's Missing? Although this class will do a job effectively for us, with more time and space, we Because PHP is loosely typed, it is our responsibility to make sure that parameters the data functions covered in Hour 16, "Working with Data." We might also want to make the Table object a little more flexible, adding methods to sort the rows according to the values in any column before we output, for example. Why a Class? manipulating arrays ourselves as and when we need to? It certainly isn't efficiency. We've added overheads to the process of storing and retrieving information. No First, this code is reusable. It has a clear purpose— to represent data in a certain way, and we can now slot it into any project that needs data stored and output in this way. Second, a Table object is active. We can ask it to output its data without bothering to write code to loop through its table_array property. tf or So, what's better about using an object to achieve this task than simply co m er cia l, passed to our methods are the type we are expecting. For this purpose, we can use http://i-tronics.blogspot.com/ us e might have added some features and safeguards. fo re du ca tio n on ly 140 Third, we've built an interface to the object's functionality. If we decide later to optimize the code in the class, we can do so without disturbing the rest of the project, as long as the same methods remain, expecting the same arguments and returning the same data types. Finally, we can build classes that inherit, extend, and override its functionality. This To create a class that inherits functionality from a parent class, we need to alter our Listing 8.6: Creating a Class That Inherits from Another 1: 2: 3: Listing 8.6 4: 5: 6: name
"; } co $this->name = $n; } m er var $name = "harry"; cia l, http://i-tronics.blogspot.com/ us e fo re du ca class declaration slightly. Listing 8.6 returns to our simple example. tio n Inheritance on ly makes object-oriented code truly cool. 141 21: 22: 23: 24: 25: $test = new second_class("son of harry"); 26: $test->sayHello(); 27: // outputs "Hello my name is son of harry" 28: ?> 29: 30: } { In addition to the simple first_class class, we have created an even more basic second_class class. Notice the extends clause in the class declaration. This means that a second_class object inherits all the functionality laid down in the first_class class. Any second_class object will have a sayHello() method and a name property just as any first_class object would. If that's not enough, there's even more magic to be found in Listing 8.6. Notice that we didn't define a constructor method for the second_class class. So, how was the name property changed from the default, "harry" to the value passed to the second_class class, "son of harry"? Because we didn't provide a constructor, the Note If a class extending another doesn't contain a constructor method, the parent class's constructor method will be called automatically when a child object is created. This feature is new in PHP4. first_class class's constructor was automatically called. No The second_class class currently creates objects that behave in exactly the same methods of their parents, allowing objects instantiated from them to behave differently, while otherwise retaining much of the same functionality. Listing 8.7 gives the second_class class its own sayHello() method. Listing 8.7: The Method of a Child Class Overriding That of Its Parent 1: way as first_class objects. In object-oriented code, child classes can override the tf or Overriding the Method of a Parent Class co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 142 2: 3: Listing 8.7 4: 5: 6: name = $n; } function sayHello() { print "Hello my name is $this->name
"; 20: class second_class extends first_class 22: 23: 24: 25: 26: function sayHello() No 27: 28: $test = new second_class("son of harry"); 29: $test->sayHello(); 30: // outputs "I'm not going to tell you my name" 31: ?> tf or } co { print "I'm not going to tell you my name
"; } m er 21: { cia l, http://i-tronics.blogspot.com/ us } e fo re du ca tio n on ly 143 32: 33: The sayHello() method in the second_class class is called in preference to that in the parent class. Calling an Overridden Method the benefit of your own additions. Object-oriented programming allows you to have the method in the first_class class that it has overridden. Listing 8.8: Calling an Overridden Method 1: 2: 3: Listing 8.8 4: 5: 6: name
"; } co function first_class( $n ) { $this->name = $n; } m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca your cake and eat it too. In Listing 8.8, the second_class's sayHello() method calls tio Occasionally, you will want the functionality of a parent class's method, as well as n on ly 144 20: class second_class extends first_class 21: 22: 23: 24: 25: 26: 27: 28: 29: $test = new second_class("son of harry"); 30: $test->sayHello(); } { function sayHello() { print "I'm not going to tell you my name -- "; first_class::sayHello(); } 31: // outputs "I'm not going to tell you my name -- Hello my name is son of harry" 32: ?> 33: 34: By using the syntax parentclassname::methodname() we can call any method that we have overridden. This syntax is new to PHP4— the same code will result in a parse error with PHP3. Inheritance: An Example from the Table class created in Listing 8.5. The new class will be called HTMLTable No and will be designed to overcome the deficiencies of Table's output() method. Defining HTMLTable's Properties HTMLTable will format the data that it stores courtesy of Table's functionality using a standard HTML table. For this example, we will allow an HTMLTable's user to tf another. Now we can use some of these techniques to create a class that inherits or You've seen how one class can inherit, override, and extend the functionality of co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 145 change the CELLPADDING argument of the TABLE element and the BGCOLOR argument of the TD element. A real-world example should allow for many more changes than this. class HTMLTable extends Table { var $bgcolor; var $cellpadding = "2"; } We have defined a new class and established that it will inherit from Table by using cellpadding a default value of 2. Creating the Constructor You have already seen that a parent class's constructor is called automatically if you don't define a constructor for a child class. In this case, however, we want to do more work with our constructor than has already been written for the Table class: function HTMLTable( $headers, $bg="#ffffff" ) Table::Table($headers); $this->bgcolor=$bg; } string becomes our bgcolor property, and we give it a default value, making it an to it. Laziness is a virtue in programming, so we let the Table class's constructor do its thing and worry no more about it. We initialize the HTMLObject's bgcolor No property. tf Note If a child class is given a constructor method, the parent's constructor is no longer called implicitly. The child class's constructor must explicitly call that of its parent. or optional argument. We call the Table class's constructor, passing the $header array co The HTMLTable constructor accepts an array of column names and a string. The m er cia l, { http://i-tronics.blogspot.com/ us e fo re du ca the extends clause. We create two properties, bgcolor and cellpadding, giving tio n on ly 146 The setCellpadding() Method A child class can of course create its own entirely new methods. setCellpadding() allows a user to change the cellpadding property from the default. Of course, it object, but this is not good practice on the whole. As a rule of thumb, it is best to complex version of this class, the setCellpadding() method might need to change Unfortunately, there is no neat way of enforcing privacy in PHP4. function setCellpadding( $padding ) { $this->cellpadding = $padding; } The Output() Method would be perfectly possible to set the cellpadding property directly from outside the create methods that will change properties on behalf of an object's user. In a more other properties to reflect the change made to the cellpadding property. The Output() method completely overrides the equivalent method in the Table class. formatting: function output() { print "cellpadding\" border=1>"; foreach ( $this->headers as $header ) No tf or print ""; foreach ( $this->table_array as $row=>$cells ) { print ""; foreach ( $cells as $cell ) print ""; print ""; } co m er cia l, It outputs data according to exactly the same logic as its parent, adding HTML table http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 147 print "
bgcolor\">$header
bgcolor\">$cell
"; } The output() method should be fairly clear if you understood the Table class's version. We loop through both the header and table_array arrays, outputting each to the browser. Crucially, though, we format the data into a table, using the cellpadding and bgcolor properties to change the spacing and color of the table that the end user sees. The Table and HTMLTable Classes in Their Entirety instantiate an HTMLTable object, change its cellpadding property, add some data, and call its ouptut() method. In a real-world example, we would probably get our row data directly from a database. Listing 8.9: The Table and HTMLTable Classes 1: 2: 3: testing objects 4: 5: 6: 7: class Table 8: 9: 10: 11: 12: { var $table_array = array(); var $headers = array(); No 13: tf 14: 15: 16: 17: or var $cols; function Table( $headers ) { $this->headers = $headers; $this->cols = count ( $headers ); } co m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca Listing 8.9 brings the entire Table and HTMLTable examples together. We also tio n on ly 148 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: function addRowAssocArray( $row_assoc ) { function addRow( $row ) { if ( count ($row) != $this->cols ) return false; array_push($this->table_array, $row); return true; } if ( count ($row_assoc) != $this->cols ) return false; $row = array(); foreach ( $this->headers as $header ) { $row_assoc[$header] = " "; $row[] = $row_assoc[$header]; } } function output() tf No 44: 45: or co { print "
"; foreach ( $this->headers as $header ) print "$header print "\n"; foreach ( $this->table_array as $y ) { "; 46: 47: m er array_push($this->table_array, $row) ; cia l, http://i-tronics.blogspot.com/ us if ( ! isset( $row_assoc[$header] )) e fo re du ca tio n on ly 149 48: 49: 50: 51: 52: 53: 54: 55: 56: class HTMLTable extends Table 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: { var $bgcolor; var $cellpadding = "2"; } foreach ( $y as $xcell ) print "$xcell "; print "\n"; } print "
"; } function HTMLTable( $headers, $bg="#ffffff" ) { Table::Table($headers); $this->bgcolor=$bg; } function setCellpadding( $padding ) { } tf No or 74: 75: 76: 77: co function output() { print "cellpadding\" border=1>"; foreach ( $this->headers as $header ) print ""; foreach ( $this->table_array as $row=>$cells ) { print ""; foreach ( $cells as $cell ) m er $this->cellpadding = $padding; cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly 150 78: 79: 80: 81: 82: 83: } print ""; print ""; } print "
bgcolor\">$header
bgcolor\">$cell
"; } 84: $test = new HTMLTable( array("a","b","c"), "#00FF00"); 85: $test->setCellpadding( 7 ); 86: $test->addRow( array(1,2,3)); 87: $test->addRow( array(4,5,6)); 88: $test->addRowAssocArray( array ( b=>0, a=>6, c=>3 )); 89: $test->output(); 90: ?> 91: 92: You can see the output from Listing 8.9 in Figure 8.2. No Figure 8.2: The HTMLTable object in action. Why Use Inheritance? So, why did we split Table from HTMLTable? Surely we could have saved ourselves time and space by building HTML table capabilities into the Table class? The answer lies in flexibility. http://i-tronics.blogspot.com/ tf or co m er cia l, us e fo re du ca tio n on ly 151 Imagine that a client gave you the brief to create a class that can maintain a table of fields, organized in named columns. If you had built a monolithic class that collected and stored the data, customized HTML, and output the result to the browser, all would seem to be well. If the same client came back to you and asked whether the code could be adapted some more methods and properties to make it do this too. A week or so later, the client realizes that she would like the code to be able to send data out as an email, and while you're at it, the company intranet uses a subset of XML; could this be accommodated too? At this stage, including all the functionality considering a complete rewrite of the code. in a single class is beginning to look a little unwieldy, and you would already be Let's try this scenario out with our Table and HTMLTable examples. We have already substantially separated formatting the data from acquiring and preparing it. When our client asks that the code should be capable of outputting to a file, we only need to create a new class that inherits from Table. Let's call it FileTable. We need make XMLTable. Figure 8.3 illustrates the relationship between these classes. no changes at all to our existing code. The same would be true for MailTable and What's more, we know that any object that inherits from Table will have an output() loop through the lot, calling output() without worrying about the mechanics. From a simply by repeatedly calling output()! No Summary It is not possible to introduce you to all the aspects of object-oriented programming in one short hour, but I hope I have introduced you to some of the possibilities. The extent to which you use objects and classes in your projects is a matter of choice. It is likely that heavily object-oriented projects will be somewhat more tf or single array of Table-derived objects, we can write emails, HTML, XML, or plain text, co method, so we can group a bunch of them into an array. When we're ready, we can m er Figure 8.3: The relationship between the Table class and multiple child classes. cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio n on ly additionally to write neatly formatted data to a text file, you could probably add 152 resource-intensive at runtime than more traditional code. However, effective deployment of object-oriented techniques can significantly improve the flexibility and organization of your code. Throughout this hour, you learned how to create classes and instantiate objects from them. You learned how to create and access properties and methods. Finally, classes. you learned how to build new classes that inherit and override the features of other understand object-oriented programming to become tio a Q This hour introduced some unfamiliar concepts. Do I really need to good PHP programmer? A The short answer is no. Most PHP scripts use little or no object-oriented code at all. The object-oriented approach won't help you do things that you couldn't otherwise achieve. The benefits of object-oriented programming lie in the organization of your scripts, their reusability, and their extensibility. Even if you decide not to produce object-oriented code, however, you may need to decipher third-party programs that contain classes. This hour should help you understand such code. Q I'm confused by the special variable $this. A Within a class, you sometimes need to call the class's methods or access its properties. By combining the $this variable and the -> operator, you can do both. The $this variable is the handle a class is automatically given to refer to itself and to its components. The Workshop provides quiz questions to help you solidify your understanding of the material covered. Try to understand the quiz answers before continuing to the next No Quiz tf hour's lesson. Quiz answers are provided in Appendix A. How would you declare a class called emptyClass() that has no methods or properties? Given a class called emptyClass(), how would you create an object that is an instance of it? or co Workshop m er cia l, http://i-tronics.blogspot.com/ us e fo re du ca n Q&A on ly 153 How can you declare a property within a class? How would you choose a name for a constructor method? How would you create a constructor method in a class? How would you create a regular method within a class? How can you access and set properties or methods from within a class? How would you access an object's properties and methods from outside the object's class? functionality from another class? Activities What should you add to a class definition if you want to make it inherit Create a class called baseCalc() that stores two numbers as properties. Give it a calculate() method that prints the numbers to the browser. Create a class called addCalc() that inherits its functionality from baseCalc(). Override the calculate() method so that the sum of the properties is printed to the browser. Repeat activity 2, for a class called minusCalc(). Give minusCalc() a calculate to the browser. No tf or co m er cia l, http://i-tronics.blogspot.com/ us e method that subtracts the first property from the second, outputting the result fo re du ca tio n on ly 154 Hour 9: Working with Forms Overview Until now, all examples in this book have been missing a crucial dimension. You can set variables and arrays, create and call functions, and work with objects. All this is meaningless if users can't reach into a language's environment to offer it user input. On the World Wide Web, HTML forms are the principal means by which substantial amounts of information can pass from the user to the server. PHP is designed to acquire and work with information submitted via HTML forms. In this hour, you will learn How to get and use environment variables How to access information from form fields How to work with form elements that allow multiple selections How to create a single document that contains both an HTML form and the PHP code that handles its submission How to save state with hidden fields How to redirect the user to a new page handle them Before you actually build a form and use it to acquire data, you need to make a small A global variable is any variable declared at the "top level" of a script — that is, declared outside a function. All functions are made available in a built-in associative No array called $GLOBALS. This is useful in Listing 9.1 because we can take a peek at all our script's global variables with a single loop. Listing 9.1: Looping Through the $GLOBALS Array 1: 2: 3: Listing 9.1 Looping through the $GLOBALS array 4: tf or detour and look again at global variables. You first met these in Hour 6, "Functions." co Global and Environment Variables m er How to build HTML forms that upload files and how to write the PHP code to cia l, http://i-tronics.blogspot.com/ us e fo re du ca tio information. In this hour, you will look at strategies for acquiring and working with n on ly 155 5: 6: $value ) 11: 12: 13: 14: ?> 15: 16: { print "\$GLOBALS[\"$key\"] == $value
"; } We declare three variables and then loop through the built-in $GLOBALS associative array, writing both array keys and values to the browser. In the output, we were able to locate the variables we defined, but we saw an awful lot more besides these. PHP automatically defines global variables that describe both the server and client environments. These are known, therefore, as environment variables. According to but they can be immensely useful. Table 9.1 lays out some common environment variables. These can be accessed as part of the $GLOBALS array, or directly. Table 9.1: Environment Variables Variable $HTTP_USER_AGENT Contains your system, server, and configuration, the availability of these variables will vary, cia l, The name and Mozilla/4.6 version of the (X11;I;Linux2.2.6-15apmac ppc) client The IP address 158.152.55.35 of the client Whether request GET or POST For requests, encoded to the URL GET name=matt&address=unknown the data the POST was No $QUERY_STRING tf or $REQUEST_METHOD co $REMOTE_ADDR $REQUEST_URI m er send appended The full address /matt/php- http://i-tronics.blogspot.com/ us e fo re du ca Example tio n on ly 156 of the request book/forms/eg9.14.html? including query name=matt string $HTTP_REFERER The address of http://www.test.com/a_page.html the page from request made was which the path to the script currently running. On my system this was as follows: This variable can also be directly accessed as the global variable $PHP_SELF. This will be useful in many of the examples in this hour. We will often include the HTML forms we use in the same page as the PHP code that analyzes the content they submit. We can use $PHP_SELF as the string assigned to the HTML FORM element's ACTION argument, saving us the trouble of hard-coding the name of the page. PHP's $GLOBALS array will become useful in other ways as well. A Script to Acquire User Input For now, we'll keep our HTML separate from our PHP code. Listing 9.2 builds a simple HTML form. 1: 2: 4: 5: Listing 9.2: A Simple HTML Form 3: Listing 9.2 A simple HTML form 7: No 10: 11:
12: 13: 14: 15: tf 8:
9: 11:
13: