Join 307,175 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,558 people online right now. Registration is fast and FREE... Join Now!
This morning I was doing some reading on advanced PHP topics, you know, things to help me become a better PHP programmer, when I ran across this article. I must say it has some very valid and interesting points (For those who do not feel like reading here are the 10 tips)
Use a SQL Injection cheatsheet (they link to this one)
Know the Difference Between Comparison Operators
Shortcut the else
Drop those Brackets
Favor str_replace() over ereg_replace() and preg_replace()
Use Ternary Operators
Memcached
Use a Framework
Use the Suppression Operator Correctly
Use isset instead of strlen
I think that's a pretty good list of tips to help any PHP programmer. Do our PHP gurus here have anything else they would like added to this list as we go along? This should make a good discussion
it says watch out for #3 and #4 because it affects readability and I agree.
I do catch flack from other programmers sometimes for using brackets when I don't need to, but It just makes everything look nicer when I have to come back to it.
I personally would love to have an end all beat all SQL injection object to call on any data. Mine currently isn't that great. Anyone got any good suggestions.
Meh. A few of them are good, but some are just wrong and stupid. Plus many of them are at best beginner to intermediate level tips, not advanced. Personally, I wouldn't recommend that list to anybody.
I do like the recommendations for frameworks and memcache, though. Even if you don't actually use a canned framework, I don't think you can call yourself an "advanced" PHP developer if you don't at least have some understanding of what they offer and when it's helpful and appropriate to use them. And Memcached...well, that's pretty much a given for anybody who has to scale. Though from the description, I get the impression that the author doesn't really know much about Memcache himself (for instance, he seems to think it's just for databases).
My biggest problem is that a lot of that list seems to focus on micro-optimizations. For instance, they even said that was the rationale for poitns 3 and 4. To me, part of being an "advanced" programmer is understanding what optimizations are useful and appropriate, and this type of micro-optimization is seldom either one.
10 is definitely the dumbest thing i've heard in a LONG time >_> If you need the length would you really setup a bunch of if statements to check it constantly?
10 is definitely the dumbest thing i've heard in a LONG time >_> If you need the length would you really setup a bunch of if statements to check it constantly?
I think it meant people were using strlen to see if variables were set instead of isset...
I think it meant people were using strlen to see if variables were set instead of isset...
Actually no. If you read the link in the article, they're talking about using isset() to determine if a string is at least a certain length. In other words, they're saying that if you want to check if a string is at least 10 characters long, using isset($string[9]) is faster than using strlen($string) >= 10.
Personally, I'll grant them that that's kind of a clever trick, but I don't think it's particularly useful in general. How often do you really check the minimum length of a string that it's going to become a performance bottleneck? It might come in handy in certain very narrow situations, but it's generally much clearer just to use strlen() and nobody's going to notice the extra few milliseconds.
11. Don't call/load other classes/files from your models or views. Do it from your controller.
After all, if you've got a situation where your controlling code calls a class function which calls another class or loads another file which calls yet another class or loads yet another file, then you're basically doing procedural programming, aren't you?
Isn't it much easier to follow if the controller calls/loads a class which does its thing, then the controller calls the next class? Isn't that what the controlling code is for: deciding what classes and methods get called and when?
This post has been edited by CTphpnwb: 6 Aug, 2009 - 08:48 AM
I think it meant people were using strlen to see if variables were set instead of isset...
Actually no. If you read the link in the article, they're talking about using isset() to determine if a string is at least a certain length. In other words, they're saying that if you want to check if a string is at least 10 characters long, using isset($string[9]) is faster than using strlen($string) >= 10.
Personally, I'll grant them that that's kind of a clever trick, but I don't think it's particularly useful in general. How often do you really check the minimum length of a string that it's going to become a performance bottleneck? It might come in handy in certain very narrow situations, but it's generally much clearer just to use strlen() and nobody's going to notice the extra few milliseconds.
I didn't read that part in the article at all, guess I was wrong XD lol
I'd say #3 is completely wrong, in that it's slower and clunker. Just because the writer's syntax can't get out of the way.
3 is a bit clunkier. It's not necessarily slower though. If the condition is true more than 50% of the time, it's slower, otherwise it's faster, in terms of program execution. It's perhaps a little less readable. But it highlights another point.
CODE
if (condition) { $x = 1; } else { $x = 2; }
will flag errors in some IDEs (like Netbeans) if $x is not previously declared. As it should - it's a block scoping error which just happens to still work in PHP. The syntax he suggests forces $x to be defined in the scope of the function, and as such I would say there's some merit to it.