Vince889 Posted September 9, 2010 Share Posted September 9, 2010 Hey PF. I'm trying to build some better coding habits, so what tips do you guys have? Of course there are obvious things such as well-commented code, neat structure, etc... But I'm looking for some more "TECHNICAL" tips that might be unspoken. For example, when is a good time to use a variable, opposed to a constant? I notice that a lot of programmers define their database settings with define(), while others just use a $variable. Is this personal preference or maybe its something I'm missing? I'm looking for ALL tips. Any information that you have, please share it. It will help make me (And anyone else reading this), a better coder. Thanks in advance. and if this is not the proper forum, please move it! =] Quote Link to comment https://forums.phpfreaks.com/topic/213011-trying-to-build-better-coding-habits/ Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 I learned something yesterday which I can share, when using variables in a double quoted string its wise to place {} around it. like: $name; echo "hello {$name}, how are you doing?"; This is to avoid problems when parsing the code. Quote Link to comment https://forums.phpfreaks.com/topic/213011-trying-to-build-better-coding-habits/#findComment-1109402 Share on other sites More sharing options...
Vince889 Posted September 9, 2010 Author Share Posted September 9, 2010 I learned something yesterday which I can share, when using variables in a double quoted string its wise to place {} around it. like: $name; echo "hello {$name}, how are you doing?"; This is to avoid problems when parsing the code. Would this be much more logical? <? echo "Hello, I am ".$name.""; ?> Or are they fairly the same? Quote Link to comment https://forums.phpfreaks.com/topic/213011-trying-to-build-better-coding-habits/#findComment-1109406 Share on other sites More sharing options...
KevinM1 Posted September 9, 2010 Share Posted September 9, 2010 The only time you need to use {} in double-quoted strings is when you want to print an array variable or when you need to pluralize something: $beer = 'Coors Light'; echo "I had five $beers"; // <-- won't work echo "I had five {$beer}s"; // <-- works For variable vs. constant, it comes down to assignment. Will you be capturing/generating a value? If so, use a variable. Why? Variables can be assigned to. Constants, once defined, are exactly that - constant. You can't overwrite one with another value. The biggest, best habit you can get into is properly structuring your files and applications. While PHP gives one the ability to switch between code and markup on the fly, it's generally not a good idea to do so. Think about it like this: How many times have you written or read code that has PHP echoing out HTML, which has JavaScript inside of that written in an inline (meaning within an element: <a href onclick="blah"...>) manner? Or PHP if-else statements, or, even better, while-loops that exit PHP to print lines of HTML right there? And does any of that sound like it would be fun to debug, edit, maintain, or extend? So, first, the consensus way to structure things is to do ALL of your PHP processing - form handling, db interactions, generating/building results from all of that activity, and any header redirects - first, then output the results of that processing. The classic sticky form (google it, if you don't know it) is the textbook example, but ALL PHP should be written like that - from one page contact forms to apps as large as Facebook. Process, then output. Two, some other terms: Separation of Concerns: Your systems are best served when they are broken down into layers, and each layer has a well-defined job to do. Your HTML should only have PHP statements that output data. Logic/processing should be done in the PHP script proper. Similarly, CSS should be linked from external files, and JavaScript should be written in an unobtrusive (read: no JavaScript written within HTML elements other than <script></script>) manner. This keeps all of your components - back end logic, client side logic, and display instructions - in their own self-contained areas, making debugging, editing, maintaining, and extending your projects a hell of a lot easier. DRY: Don't Repeat Yourself. Find yourself writing the same code over and over again within a file? That's a sign that you're doing it wrong. Refactor your code. See if you can reduce it by making the repeated part a function, or see if you can write it in a clearer, more concise manner. Well written code reads like a book. One other thing, not related to coding: if you're trying to research a problem, the very first place you should go is the PHP manual. Chances are, there's a built-in function that does what you want to do. So, save this link and use it often: http://www.php.net/quickref.php I'm sure others have more useful tips as well. Quote Link to comment https://forums.phpfreaks.com/topic/213011-trying-to-build-better-coding-habits/#findComment-1109419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.