PHP 5 for Dummies Free Open Book

PHP 5 for Dummies

Previous PageNext Page
Chapter 8
Reusing PHP Code
In This Chapter
Including files in scripts
Understanding security for included files
Writing functions
Using functions
O
ften scripts need to perform the same actions in several different loca-
tions in the script. For example, a script may need to get data from a
database several different times. It may even be the case that you use the
same code in different scripts. If you find yourself typing the same ten lines of
code over and over (or cutting and pasting it repeatedly), you can move that
code into a separate file and get it from that file whenever you need it. Here
are several reasons to reuse code:
Less typing:Less work is always a good reason for anything.
Debug once:You can write the code once, debug it so you know it works,
and then use it whenever you need it. It’s rare to write code that doesn’t
have a typo or two in it, let alone occasional peculiar logic, so code always
has to be debugged. It saves time to use proven code when possible,
instead of writing new code that will have to be debugged.
Easier to understand:A shorter script that is less cluttered with code is
easier for people to read and understand. For example, one line in your
script that says 
getData()
is easier to understand than the ten lines
that actually get the data.
Easier to maintain:If you reuse code and you need to change something
in the code, you only need to change it in one external file, instead of
having to find and change it in a dozen places in your script. For exam-
ple, if you change the name of your database, you can change the name
in one file, rather than having to change it repeatedly in many scripts.
You can reuse code two ways: by inserting a file containing code into a script
or by writing and calling a function. In this chapter, you find out how to use
both methods. 
Previous PageNext Page


     Main Menu
PHP 5 for Dummies
Table of Contents
Introduction
Part I: Say Hello to the PHP Scripting Language
Part II: Variables and Data
Part III: Basic PHP Programming
Using Array and 0 - Page 145
Chapter 7: Controlling the Flow of the Script
Chapter 8: Reusing PHP Code
Inserting Code in Your Script
Creating Reusable Code (Functions)
Chapter 9: Object-Oriented Programming Meets PHP
Part IV: Common PHP Applications
Part V: The Part of Tens
Part VI: Appendixes
Index
Team DDU


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele