Chapter 1: Improving Your PHP Programs
In This Chapter
Including helpers automatically
Reusing code
In earlier chapters, you’ve seen how to program in PHP. You’ve seen how to create a program, how to loop, set up conditionals, and more. All of that knowledge has enabled you to create PHP programs that work well on the web. But you can make them even better, even easier to use, and that’s what this chapter is all about.
In this chapter you’ll see how to improve and extend your PHP programs and how to create and use helper functions automatically. You’ll also see ways to reuse code rather than reinventing it every time you need it.
Automatically Including Helper Functions
Once your programs reach a certain length and complexity, you find that there are a lot of includes
and require_once()
functions. Each time you make a new file or try to make something into a common function, you also need to go back through all the programs and add a new require_once
. That can quickly become monotonous. Luckily, there's a way around it.
Using auto_prepend_file
You can automatically prepend a PHP file so that its code is executed before the actual file being requested. In other words, if you send a visitor to a URL similar to http://www.
example.com
/login.php
, you can use auto_prepend_file
to always ...