[ Pobierz całość w formacie PDF ]
.ItPhas features to aid you in programming the tasks needed to developdynamic Web applications.PHP is in use on more than 20 million domains(according to the Netcraft survey at www.php.net/usage.php).Its popu-larity continues to grow, so it must be fulfilling its function pretty well.The PHP language syntax is similar to the syntax of C, so if you have experi-ence with C, you ll be comfortable with PHP.PHP is actually simpler than Cbecause it doesn t include some of the more difficult concepts of C con-cepts not required to program Web sites.In this chapter, we describe the basics of writing PHP scripts the rulesthat apply to all PHP statements.Consider these rules similar to generalgrammar and punctuation rules.In the remaining chapters in this minibook,you find out about specific PHP statements and features and how to writePHP scripts to perform specific tasks.How PHP WorksThe PHP software works with the Web server.The Web server is the soft-ware that delivers Web pages to the world.When you type a URL into yourWeb browser s address bar, you re sending a message to the Web server atthat URL, asking it to send you an HTML file.The Web server responds bysending the requested file.Your browser reads the HTML file and displaysthe Web page.You also request a file from the Web server when you click alink in a Web page.In addition, the Web server processes a file when youclick a Web page button that submits a form.11_167779 bk02ch01.qxp 12/17/07 8:08 PM Page 104104 How PHP WorksHow the Web server processes PHP filesWhen a browser is pointed to a regular HTML 3.When it encounters a PHP opening tag, thefile with an.html or.htm extension, the Web server switches to PHP mode.This isWeb server sends the file, as is, to the browser.sometimes called escaping from HTML.The browser processes the file and displays The Web server then assumes that allthe Web page described by the HTML tags in statements are PHP statements and exe-the file.When a browser is pointed to a PHP file cutes the PHP statements.If there is(with a.phpextension), the Web server looks output, the server sends the output to thefor PHP sections in the file and processes them browser.instead of just sending them as is to the4.The Web server continues in PHP modebrowser.The Web server processes the PHPuntil it encounters a PHP closing tag (?>).file as follows:5.When the Web server encounters a PHP1.The Web server starts scanning the file inclosing tag, it returns to HTML mode.ItHTML mode.It assumes the statements areresumes scanning, and the cycle continuesHTML and sends them to the browser with-from Step 1.out any processing.2.The Web server continues in HTML modeuntil it encounters a PHP opening tag( $valuename ){block of statements;}PHP Basics11_167779 bk02ch01.qxp 12/17/07 8:08 PM Page 136136 Using ArraysFill in the following information:&' arrayname: The name of the array that you re walking through.&' keyname: The name of the variable where you want to store the key.keynameis optional.If you leave out $keyname =>, only the value isput into a variable that can be used in the block of statements.&' valuename: The name of the variable where you want to store thevalue.For instance, the following foreachstatement walks through the samplearray of state capitals and echoes a list:$capitals = array( CA => Sacramento , TX => Austin , OR => Salem );ksort($capitals);foreach( $capitals as $state => $city ){echo $city, $state ;}The preceding statements give the following Web page output:Sacramento, CASalem, ORAustin, TXYou can use the following line in place of the foreachline in the previousstatements:foreach( $capitals as $city )When using this foreachstatement, only the city is available for output.You would then use the following echostatement:echo $city ;The output with these changes isSacramentoSalemAustinWhen foreachstarts walking through an array, it moves the pointer to thebeginning of the array.You don t need to reset an array before walkingthrough it with foreach.11_167779 bk02ch01.qxp 12/17/07 8:08 PM Page 137137Using ArraysMultidimensional arraysIn the earlier sections of this chapter, we describe arrays that are a single listof key/value pairs.However, on some occasions, you might want to storevalues with more than one key [ Pobierz całość w formacie PDF ]