13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5<br />

Reusing Code <strong>and</strong> Writing<br />

Functions<br />

THIS CHAPTER EXPLAINS HOW REUSING CODE leads to more consistent, reliable, maintainable<br />

code, with less effort.We demonstrate techniques for modularizing <strong>and</strong> reusing<br />

code, beginning with the simple use of require() <strong>and</strong> include() to use the same code<br />

on more than one page.We explain why these includes are superior to server-side<br />

includes.The example given here covers using include files to get a consistent look <strong>and</strong><br />

feel across your site.We also explain how you can write <strong>and</strong> call your own functions<br />

using page <strong>and</strong> form generation functions as examples.<br />

Key topics covered in this chapter include<br />

n The advantages of reusing code<br />

n Using require() <strong>and</strong> include()<br />

n Introducing functions<br />

n Defining functions<br />

n Using parameters<br />

n Underst<strong>and</strong>ing scope<br />

n Returning values<br />

n Calling by reference versus calling by value<br />

n Implementing recursion<br />

n Using namespaces<br />

The Advantages of Reusing Code<br />

One of the goals of software engineers is to reuse code in lieu of writing new code.<br />

The reason for this is not that software engineers are a particularly lazy group. Reusing<br />

existing code reduces costs, increases reliability, <strong>and</strong> improves consistency. Ideally, a new<br />

project is created by combining existing reusable components, with a minimum of<br />

development from scratch.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!