Archive for March, 2007
Working With A PHP Configuration File
TRUE or FALSE: “It’s easier to update one file or 10 files.” This statement is True.
The concept of a configuration file is that it stores data in one location that any other file on the web site can access. The data that is stored in a configuration file can be unlimited, but usually contains data such as by not limited to the following:
- MySQL Database, Username and Password
- Document Root Folder
- To use Cookies or Sessions
- Set time limit for Sessions
- Custom variables for web applications
- and etc.
Use As Many PHP Files As You Need
I’m working with a friend – helping him with some dynamic coding – on a web site. The web site was put together by a different webmaster and it functions. The clients needs have changed over time, and changes are need to the site to comply with a few legal things (it deals with housing.) As I begin looking at the PHP code, I can tell the person who programmed it was very intelligent, but fell prey to some of the things that irritate me, and create problems when trying to update the code as I’m doing now.
First, they didn’t comment any of their PHP code, second they did make use of arrays, but did a poor job of it, and they posted all of the forms back to themselves, which brings me to the point of this post. Use more than one file if you have to, and don’t be afraid to do so. Here’s the problem with posting a form back to itself.
Commenting Your PHP Code
About two years ago I started working for a local web firm. It was small, at that time there we’re only 3 of us, and I was the only PHP developer on staff. We had one client who used our services, combined with another web vendor. Normally we just spit out regular static HTML pages for them, until one day something happened, and we were passed a job that required editing some PHP code from the other vendor.
I honestly don’t remember who the client was, or what the PHP that I was editing was used for, all I remember is that it was the worst code I had ever come into contact with. Nothing made sense, the coding method was random, nothing was optimized, and I wanted to re-write the whole thing and start over. Time did not permit a total re-write, so I was stuck editing the nasty code that was before me. So as I made my edits, I started commenting the code, it went something like this…
Looping Through Arrays
One of my favorite time saving techniques is to loop through an array whether it’s an indexed or an associative array, I’ve saved more time on projects by looping through arrays vs. writing code to handle each value of the array. I bring this up because I’ve had to update too many PHP scripts written by other developers where they handle each and every value of the $_POST or $_GET arrays as individual values to be parsed.
Here’s a practical example.
Read the rest or post a comment »
There’s Always A Better Way
I’m a firm believer in the statement, “There’s always a better way.” When I started learning PHP, I assumed that what I was reading was hardcore `gospel` truth. What I realized later as I was trying to maintain my code was, there is a better way to do this. I was really irritated when my epiphany hit me. Irritated that I didn’t think of it sooner, but I was the novice and I chalked it up to learning, then I got really irritated at the authors of the PHP information I was reading because they never provided a clue, “there’s an easier way to do this…”
