Show HN: PHP Best Practices, a short guide for common and confusing PHP tasks
phpbestpractices.org
phpbestpractices.org
I wanted to compile a guide containing what can be considered the best ways of approaching such problems, with basic examples and links to follow-up reading.
If anyone has suggestions or corrections, please drop me a line! Contact info is in the page.
Could you elaborate on why you chose PDO over MySQLi to interact with MySQL? Especially with all the "specific things to do for it to work properly" at the end of the section?
The email address validation is also a great section, everyone tries to rewrite its own and fails or forgets to handle cases (such as '+' in first part). I think it would be great to show other filters examples instead of providing a link to the documentation, like how to sanitize a username entry for example?
Another interesting section would be how to handle errors properly, like the use of trigger_error(), set_error_handler() or exceptions. Seems that a lot of people are also doing their own logging/debugging/reporting system not knowing where to start?
Suggestions :
Sanitizing HTML
Please explain why you prefer htmlspecialchars over htmlentities
Please explain why you should htmlspecialchars when outputting content, but never use it when inserting to database (obvious for us, not for your reader)
Checking for null values
Please explicit that === should also be used when compairing to false (problem is the same as with null)
Please use Yoda condition in your example or even better explain why (it's also a best practice in PHP (also recommanded by WordPress Coding guidelines and several other frameworks, by example))
That means there is no need to go and tell people to convert all their <?= ?> into <?php print() ?>.
Source here : http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
I got a few tidbits to add.
"if you ever plan on deploying your PHP to a server whose configuration you can't control, then you should always use <?php ?>." I would change that to "always use <?php ?>", if you do you're safe, otherwise you can run into problems (as I have).
its a matter of opinion but I would recommend SwiftMailer over phpmailer.
And lastly, I would definately put namespacing and code organization in here somewhere, like PSR https://github.com/php-fig/fig-standards/blob/master/accepte...
liked the article!