School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

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!




10 Advanced PHP Tips To Improve Your Programming

 

10 Advanced PHP Tips To Improve Your Programming

PsychoCoder

30 Jul, 2009 - 08:56 AM
Post #1

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,939



Thanked: 519 times
Dream Kudos: 11575
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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)
  1. Use a SQL Injection cheatsheet (they link to this one)
  2. Know the Difference Between Comparison Operators
  3. Shortcut the else
  4. Drop those Brackets
  5. Favor str_replace() over ereg_replace() and preg_replace()
  6. Use Ternary Operators
  7. Memcached
  8. Use a Framework
  9. Use the Suppression Operator Correctly
  10. 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

User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 14)

born2c0de

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 09:04 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,550



Thanked: 98 times
Dream Kudos: 2825
Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
I read the article too a while back.
It is pretty useful indeed.
User is offlineProfile CardPM
+Quote Post

TechWar

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 09:45 AM
Post #3

D.I.C Regular
***

Joined: 17 Feb, 2009
Posts: 469



Thanked: 7 times
My Contributions
I like those tips, but, I also lke readability.

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.

glad you posted this though.
User is offlineProfile CardPM
+Quote Post

gregwhitworth

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 09:48 AM
Post #4

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 83 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
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.

--

Greg
User is offlineProfile CardPM
+Quote Post

Nykc

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 09:48 AM
Post #5

This script is cheap!
Group Icon

Joined: 14 Sep, 2007
Posts: 6,561



Thanked: 44 times
Dream Kudos: 375
My Contributions
Very nice find....
User is online!Profile CardPM
+Quote Post

baavgai

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 10:34 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,351



Thanked: 412 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Interesting. Some good ones. A large number are just syntax quirks, though: some to the detriment of the program.

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.

#4 is simply wrong and has lead to more errors than anything else.

I agree with #6, but like #3 and #4, it's still just style. No, #3 is still bad, regardless of style. tongue.gif

User is offlineProfile CardPM
+Quote Post

mocker

RE: 10 Advanced PHP Tips To Improve Your Programming

30 Jul, 2009 - 06:25 PM
Post #7

D.I.C Regular
Group Icon

Joined: 14 Oct, 2007
Posts: 440



Thanked: 37 times
Dream Kudos: 25
My Contributions
8 should be "use a framework when appropriate" . There are many uses for php that don't call for a full framework to be invoked.
User is offlineProfile CardPM
+Quote Post

AdaHacker

RE: 10 Advanced PHP Tips To Improve Your Programming

31 Jul, 2009 - 01:21 PM
Post #8

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 395



Thanked: 86 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

phoeniX_

RE: 10 Advanced PHP Tips To Improve Your Programming

5 Aug, 2009 - 09:56 AM
Post #9

New D.I.C Head
*

Joined: 4 Aug, 2009
Posts: 25


My Contributions
Great article. Thanks for sharing! smile.gif
User is offlineProfile CardPM
+Quote Post

JBrace1990

RE: 10 Advanced PHP Tips To Improve Your Programming

5 Aug, 2009 - 04:34 PM
Post #10

D.I.C Addict
Group Icon

Joined: 9 Mar, 2008
Posts: 602



Thanked: 46 times
Dream Kudos: 350
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: 10 Advanced PHP Tips To Improve Your Programming

5 Aug, 2009 - 07:45 PM
Post #11

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 701



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
QUOTE(JBrace1990 @ 5 Aug, 2009 - 05:34 PM) *

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...

Well, that's what I understood from it anyway...

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

AdaHacker

RE: 10 Advanced PHP Tips To Improve Your Programming

6 Aug, 2009 - 05:54 AM
Post #12

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 395



Thanked: 86 times
My Contributions
QUOTE(ShaneK @ 5 Aug, 2009 - 09:45 PM) *
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.
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: 10 Advanced PHP Tips To Improve Your Programming

6 Aug, 2009 - 08:47 AM
Post #13

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
My vote for #11:

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
User is online!Profile CardPM
+Quote Post

ShaneK

RE: 10 Advanced PHP Tips To Improve Your Programming

8 Aug, 2009 - 09:05 AM
Post #14

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 701



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
QUOTE(AdaHacker @ 6 Aug, 2009 - 06:54 AM) *

QUOTE(ShaneK @ 5 Aug, 2009 - 09:45 PM) *
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

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

moopet

RE: 10 Advanced PHP Tips To Improve Your Programming

6 Sep, 2009 - 08:02 AM
Post #15

D.I.C Regular
***

Joined: 2 Apr, 2009
Posts: 349



Thanked: 34 times
My Contributions
QUOTE(baavgai @ 30 Jul, 2009 - 05:34 PM) *

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:25PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month