PHP Best Practices
5 posts
Page 1 of 1
In this article i will explain some of the best practices you should use when writing PHP code. These are used by many coders, but if you don't agree with them, feel free to leave this page.
PHP Manual
The PHP Manual its incredibly detailed, every function is explained and tested, If that's not enough you can find user comments on almost every page.
Error Reporting
Always develop with error_reporting turned on, This way you can find and fix bugs more easily, than going through hundreds of lines of code to find the problem. Usually i add this at the top of the page error_reporting(-1);
PHP Tags
Always use the full format of the PHP tags <?php echo 'Hello'; ?>. Why? Because the short tags <? echo 'Hello'; ?> can be disabled in the configuration file, and your App won't work.
Comments
You should always comment your code (within reason, not every single line), Especially if you work in a group, but also it will make your code more maintainable and more readable.
Naming Stuff
When creating variables, functions, classes, etc... use human readable names like $PostTitle instead of $ptitle and MyFunction instead of MyFunc etc..
Objects (PHP OOP)
Learn OOP, Object Oriented Programing is one of the most important features of PHP, the ability to use and extend classes, will help you reduce code repetitions, and will make your App much more maintainable and easy to modify in the future.
Single Quotes
When you have to output plain text, always use single quotes, Because PHP will output them as they were written, without loosing time with parsing.
Use double quotes when outputting text that contains special characters (\t\r\n etc..) and variables. PHP always parses double quotes, even if they don't contain special characters or variables. This has an impact on performance, negligible for the most part, but depends on how many double quotes you have in your page.
Triple Equals
When you check equality between stuff, always use === instead of == Why? For example when doing '5' == 5 it will return true, because '5' will read as a number. But when doing '5' === 5 it will return false, because '5' is a string and 5 is a number.
So == are used to check if variables are equal, And === are use to check if variables are both equal and type identical.
Database Connection
Database connections are very expensive, so you should only use one per page, Contrary to popular belief, you CAN use one database connection to do how many operations you need 10, 50, 100 etc...
File Writing
Always check if the directory, where you plan to write your files, is writable. If its not attempt the chmod command on it. Same goes for any files you're trying to modify.
No WordPress
Stay away from WordPress, and don't use they're coding styles, as an example.
WordPress doesn't put much stock on coding style, that's why the source code looks like a bomb exploded inside. You can't make out heads or tails of it. Maybe in the future it will get better, They started adopting OOP standards.
No Google Copy
Don't just copy -> paste, code you find with Google, Take it apart and learn how it works, Then attempt to rewrite it on your own. You'll be amazed how fast you learn to code this way.
Ask For Help (the most important of all)
When you have a problem just ask someone for help, there are dozens of websites with developers from around the world, which are more then willing to help.
That's it, i really hope some of you will implement these suggestions/rules.
PHP Manual
The PHP Manual its incredibly detailed, every function is explained and tested, If that's not enough you can find user comments on almost every page.
Error Reporting
Always develop with error_reporting turned on, This way you can find and fix bugs more easily, than going through hundreds of lines of code to find the problem. Usually i add this at the top of the page error_reporting(-1);
PHP Tags
Always use the full format of the PHP tags <?php echo 'Hello'; ?>. Why? Because the short tags <? echo 'Hello'; ?> can be disabled in the configuration file, and your App won't work.
Comments
You should always comment your code (within reason, not every single line), Especially if you work in a group, but also it will make your code more maintainable and more readable.
Naming Stuff
When creating variables, functions, classes, etc... use human readable names like $PostTitle instead of $ptitle and MyFunction instead of MyFunc etc..
Objects (PHP OOP)
Learn OOP, Object Oriented Programing is one of the most important features of PHP, the ability to use and extend classes, will help you reduce code repetitions, and will make your App much more maintainable and easy to modify in the future.
Single Quotes
When you have to output plain text, always use single quotes, Because PHP will output them as they were written, without loosing time with parsing.
Use double quotes when outputting text that contains special characters (\t\r\n etc..) and variables. PHP always parses double quotes, even if they don't contain special characters or variables. This has an impact on performance, negligible for the most part, but depends on how many double quotes you have in your page.
Triple Equals
When you check equality between stuff, always use === instead of == Why? For example when doing '5' == 5 it will return true, because '5' will read as a number. But when doing '5' === 5 it will return false, because '5' is a string and 5 is a number.
So == are used to check if variables are equal, And === are use to check if variables are both equal and type identical.
Database Connection
Database connections are very expensive, so you should only use one per page, Contrary to popular belief, you CAN use one database connection to do how many operations you need 10, 50, 100 etc...
File Writing
Always check if the directory, where you plan to write your files, is writable. If its not attempt the chmod command on it. Same goes for any files you're trying to modify.
No WordPress
Stay away from WordPress, and don't use they're coding styles, as an example.
WordPress doesn't put much stock on coding style, that's why the source code looks like a bomb exploded inside. You can't make out heads or tails of it. Maybe in the future it will get better, They started adopting OOP standards.
No Google Copy
Don't just copy -> paste, code you find with Google, Take it apart and learn how it works, Then attempt to rewrite it on your own. You'll be amazed how fast you learn to code this way.
Ask For Help (the most important of all)
When you have a problem just ask someone for help, there are dozens of websites with developers from around the world, which are more then willing to help.
That's it, i really hope some of you will implement these suggestions/rules.
You can find me on Facebook or on Skype mihai_92b
XTechVB wrote: Error ReportingIt's easier to use overall config in php.ini, not have this in every single file
Always develop with error_reporting turned on, This way you can find and fix bugs more easily, than going through hundreds of lines of code to find the problem. Usually i add this at the top of the page error_reporting(-1);
XTechVB wrote: PHP TagsIf you have access to config in php.ini, that shouldn't be a problem.
Always use the full format of the PHP tags <?php echo 'Hello'; ?>. Why? Because the short tags <? echo 'Hello'; ?> can be disabled in the configuration file, and your App won't work.
XTechVB wrote: Database ConnectionIt's best to use class for that in my opinion.. Just use constructor for making connection and destructor for closing it.
Database connections are very expensive, so you should only use one per page, Contrary to popular belief, you CAN use one database connection to do how many operations you need 10, 50, 100 etc...
XTechVB wrote: No WordPresstheir*
Stay away from WordPress, and don't use they're coding styles, as an example.
WordPress doesn't put much stock on coding style, that's why the source code looks like a bomb exploded inside. You can't make out heads or tails of it. Maybe in the future it will get better, They started adopting OOP standards.
Yeah, it's simpler to make set of your own functions/classes (ie. databases, multilanguage, etc) and you can easily adapt it for project
I would like to add:
Sanitize user inputted data
Never trust users with their input as they might inject malicious code into your app.. From XSS (*cough* #Shim *cough*

use print_r() to debug
you can echo or print_r data after each step to see where's the error
Track errors in error_log when you go to production
It will make it easier to remove bugs and security issues
Make private and public alphas before going live
Use power of SQL
Try to make more complicated queries, you'll have less work to do with PHP
Also +rep to #XTechVB
-Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
I was referring to projects that you can use on other servers not just your own, where you most like have little or no control over the php.ini file.
In my opinion, you shouldn't modify anything in the host's php.ini file, in order to make your App work.
In my opinion, you shouldn't modify anything in the host's php.ini file, in order to make your App work.
You can find me on Facebook or on Skype mihai_92b
#XTechVB and #Filip you have both provided useful information, I would +rep but can't. Some of this info has been helpful to know.
Thanks
Thanks
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023